2017-03-23 3 views
0

私は、ユーザー入力を受け入れ、HTTP POSTメソッドを使用してWebサイトのAPIに対して検証するJava Desktopアプリケーションで作業しています。ユーザーが入力した情報がWebサイトのデータベースと一致する場合、このメソッドは200 OKの応答コードを返すことになり、ユーザーはJavaアプリケーションの次のページに進むことができます。正しくJavaアプリケーションからhttp POSTメソッドを使用することができません

FOR THE CODE TO WORK, TWO INPUT PARAMETERS ARE REQUIRED 

    1. license key = Smx22Mar 
    2. deviceid = Laptop 
    Content Type must be: application/x-www-form-urlencoded  

たちはPOSTMANを使用して、この設定を検証するJavaアプリケーションを介してPOSTメソッドを実行する前に、データベースに対してそれをチェックし、同じ入力を受け取り、200 OKのレスポンスコードを提供します。

しかし、同じ入力パラメータがJavaアプリケーションから提供されると、204のコンテンツが見つかりませんという応答コードが返されます。

アプリケーション全体のコードは、以下である:

// FXML CONTROLLER CLASS パッケージ認証。

import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.net.URL; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.ResourceBundle; 
import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicNameValuePair; 
import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.fxml.Initializable; 
import javafx.scene.control.TextField; 


public class FXMLDocumentController implements Initializable { 

    @FXML private TextField productKeyField; 
    @FXML private TextField deviceIdField; 

    @FXML private void Validate(ActionEvent event){ 

    String authKey=productKeyField.getText(); 
    String devicetype= deviceIdField.getText(); 

    boolean serverResponse=getServerKeyStatus(authKey,devicetype); 
    System.out.println("Server Response"+serverResponse); 

    if(serverResponse){ 
    //THIS IS THE DESIRED OUTPUT,WITH A STATUS OF 200 OK 
    System.out.println("License key is Authorised-------ACCESS GRANTED------"); 

    try { 
    } catch (Exception e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); } 
    } else { 
    System.out.println("Unauthorized License key"); 
    } 
    } 

    public static boolean getServerKeyStatus(String authkey,String devicetype){ 
    FXMLDocumentController http=new FXMLDocumentController(); 
    int status_code = 0; 
    try { 
     System.out.println(" Key is:"+authkey+ " Device Type is "+ devicetype); 
     status_code=http.sendPost(authkey,devicetype); 
     } catch (Exception e) { 
     System.out.println("Exception while license key "+ e.getMessage()); 
     } 
     if(status_code==200){ 
     //Logic to go to the next page 
     return true; 
     } 
     else{ 
     System.out.println("Please enter the right KEY to proceed"); 
     return false; 
     } 
} 


    private int sendPost(String authkey,String devicetype) throws Exception { 

     final String USER_AGENT = "Mozilla/5.0"; 
     int status_code=0; 
     try { 

     String url = "http://www.example.com/api/software/activate"; 

     HttpClient client = new DefaultHttpClient(); 
     HttpPost post = new HttpPost(url); 
     post.setHeader("User-Agent", USER_AGENT); 
     List<NameValuePair> urlParameters = new ArrayList<>(); 
     urlParameters.add(new BasicNameValuePair("licensekey",authkey)); 
     urlParameters.add(new BasicNameValuePair("deviceid",devicetype)); 

     HttpResponse response = client.execute(post); 
     response.setEntity(new UrlEncodedFormEntity(urlParameters)); 

     System.out.println("\nSending 'POST' request to URL : " + url); 
     System.out.println("Post parameters : " + post.getEntity()); 
     System.out.println("Response Code : " + response.getStatusLine().getStatusCode()); 
     status_code= response.getStatusLine().getStatusCode();         

     BufferedReader rd = new BufferedReader(
     new InputStreamReader(response.getEntity().getContent())); 

     StringBuffer result = new StringBuffer(); 
     String line = ""; 
     while ((line = rd.readLine()) != null) { 
     result.append(line); 
     } 
     System.out.println(result.toString()); 
     } catch (Exception e) { 
     } 
     return status_code; 
    } 


    public void initialize(URL url, ResourceBundle rb) { 
     // TODO 
    } 
} 

// FXML CODE FOR GUI

<?xml version="1.0" encoding="UTF-8"?> 

<?import javafx.scene.control.Button?> 
<?import javafx.scene.control.Label?> 
<?import javafx.scene.control.TextField?> 
<?import javafx.scene.layout.AnchorPane?> 

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" style="-fx-background-color: lightblue;" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="authentication.FXMLDocumentController"> 
    <children> 
     <Button layoutX="144.0" layoutY="147.0" onAction="#Validate" text="Validate" /> 
     <Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" /> 
     <TextField fx:id="productKeyField" layoutX="121.0" layoutY="52.0" /> 
     <TextField fx:id="deviceIdField" layoutX="121.0" layoutY="100.0" /> 
     <Label layoutX="20.0" layoutY="56.0" text="Authorisation key" /> 
     <Label layoutX="36.0" layoutY="104.0" text="Device type" /> 
    </children> 
</AnchorPane> 

// MAIN CLASS

public class Authentication extends Application { 

    @Override 
    public void start(Stage stage) throws Exception { 
     Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml")); 
     Scene scene = new Scene(root); 
     stage.setScene(scene); 
     stage.show(); 
    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     launch(args); 
    } 

} 

私は、なぜ、入力パラメータを理解することができないんだ、したがって、これらのサーブレット・メソッドに新しいですPOSTMANを介して入力され、JavaアプリケーションからのPOSTメソッド呼び出しでは機能しません。

非常に長いコードと質問は申し訳ありません。私が間違っているところや、これを修正する方法を教えてもらえますか?前もって感謝します。

答えて

0

これらのApacheツールを前回使用したときからAPIが変更されました。だから私が示していることは、正確な答えではないかもしれません。しかし、私はあなたがあなたのパラメータを投稿ではなくレスポンスに入れているように見えます。さらに、間違った順序でそれをやっています。

悪い:

HttpResponse response = client.execute(post); 
    response.setEntity(new UrlEncodedFormEntity(urlParameters)); 

は(おそらく)○:

post.setEntity(new UrlEncodedFormEntity(urlParameters)); 
    HttpResponse response = client.execute(post); 

あなたがこの記事を見てしたいと思うかもしれ - >Sending HTTP POST Request In Java

+0

こんにちはクリス、示唆されているように、コードの順序を変更してみました。また、提案されている参考記事のコードに従って_HttpEntity entity = response.getEntity(); if else statement_theエンティティオブジェクトはヌル値を返します。 – Berch

関連する問題