3

I've an HTTP server which accepts HTTP POST requests, it is working just fine for the request made from the web-browser.

Now, I wanted make the same request from client app being developed using JavaFX-2. Please share me a code snippet just enough to pass two string type parameters Ex Username and Password from JavaFX-2 client to HTTP Server and handle the response back from server.

PS: Since I'm new to JavaFX which is relatively new, just wanted to know is there any special HTTP client API available in JavaFX or it is just as any HTTP request made from existing Java APIs.

Player_Neo
  • 1,413
  • 2
  • 19
  • 28

1 Answers1

1

The short answer - there is no difference between JavaFX and Java in this situation. Use standart Java way of sending HTTP requests.

To retrieve login and password, you can create a very simple stage, using PasswordField and TextField.

Code for JFX scene (very simple) :

@Override
public void start(Stage stage) throws Exception {
    final TextField tf = new TextField();
    final PasswordField pf = new PasswordField();
    Button b = new Button("post");
    b.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent t) {
            //Run a separate thread for posting and getting an answer.
            new Thread(new Runnable() {
                @Override
                public void run() {
                    String login = tf.getText();
                    String pswd = pf.getText();

                    //Do post (read, from link below)

                    //Update of fragment from that article about POST in Java:
                    if (entity != null) {
                        InputStream instream = entity.getContent();
                        try {
                            //If you need to update of UI here, use Platform.runLater(Runnable);
                        } finally {
                            instream.close();
                        }
                    }
                }
            }).start();

            //Erase pswd, if needed
        }
    });
    stage.setScene(new Scene(new VBox(tf, pf, b), 300, 300));
    stage.show();
}

About how to do a post, you may read here : Sending HTTP POST Request In Java

Community
  • 1
  • 1
Alexander Kirov
  • 3,624
  • 1
  • 19
  • 23