質問者さんがご提示のコードで試されている Apacheの HttpClient 5.0 ですが、その非同期版であれば、HttpAsyncClient 4.1 になるかと思います。
その説明ページ HttpAsyncClient Quick StartにGETメソッドでのサンプルコードがあります。これをもとにPOSTメソッドでフォームパラメータを扱うコードを作成し、試してみましたのでご参考までに以下に示します。
JavaFXでは試せてはいませんが、イベントハンドラーにうまく適用することで使うことができると思います。
Java
1import java.util.ArrayList;
2import java.io.IOException;
3import java.nio.CharBuffer;
4import java.util.concurrent.Future;
5import java.util.concurrent.CountDownLatch;
6import java.util.concurrent.ExecutionException;
7
8import org.apache.http.nio.IOControl;
9import org.apache.http.nio.client.*;
10import org.apache.http.nio.client.methods.HttpAsyncMethods;
11import org.apache.http.nio.client.methods.AsyncCharConsumer;
12import org.apache.http.nio.protocol.HttpAsyncRequestProducer;
13import org.apache.http.protocol.HttpContext;
14import org.apache.http.impl.nio.client.HttpAsyncClients;
15import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
16import org.apache.http.client.methods.HttpPost;
17import org.apache.http.client.entity.UrlEncodedFormEntity;
18import org.apache.http.HttpResponse;
19import org.apache.http.NameValuePair;
20import org.apache.http.message.BasicNameValuePair;
21import org.apache.http.concurrent.FutureCallback;
22
23public class HttpPostSample {
24
25 public static void main(String[] args) {
26 String url = "http://localhost:8000/cgi-bin/t1.py";
27 if (args.length > 0) {
28 url = args[0];
29 }
30
31 try {
32 run(url);
33 } catch (Exception ex) {
34 System.out.println("Exception: *****");
35 ex.printStackTrace();
36 }
37 }
38
39 private static void run(String url)
40 throws IOException, InterruptedException, ExecutionException {
41
42 CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
43 try {
44 // Start the client
45 httpclient.start();
46
47 // Execute request
48 final HttpPost request1 = new HttpPost(url);
49 ArrayList<NameValuePair> parms1;
50 parms1 = new ArrayList<NameValuePair>();
51 parms1.add(new BasicNameValuePair("p11", "v11"));
52 parms1.add(new BasicNameValuePair("p12", "v12"));
53 request1.setEntity(new UrlEncodedFormEntity(parms1, "UTF-8"));
54
55 System.out.println("#1 started...");
56 Future<HttpResponse> future = httpclient.execute(request1, null);
57 // and wait until a response is received
58 HttpResponse response1 = future.get();
59 System.out.println(request1.getRequestLine() + "->" + response1.getStatusLine());
60 System.out.println("#1 done.");
61
62 // Execute request
63 // One most likely would want to use a callback for operation result
64 final CountDownLatch latch1 = new CountDownLatch(1);
65 final HttpPost request2 = new HttpPost(url);
66
67 ArrayList<NameValuePair> params2;
68 params2 = new ArrayList<NameValuePair>();
69 params2.add(new BasicNameValuePair("p21", "v21"));
70 params2.add(new BasicNameValuePair("p22", "v22"));
71 request2.setEntity(new UrlEncodedFormEntity(params2, "UTF-8"));
72
73 System.out.println("#2 started...");
74 httpclient.execute(request2, new FutureCallback<HttpResponse>() {
75
76 public void completed(final HttpResponse response2) {
77 latch1.countDown();
78 System.out.println("completed: " + request2.getRequestLine() + "->" + response2.getStatusLine());
79 }
80
81 public void failed(final Exception ex) {
82 latch1.countDown();
83 System.out.println("failed: " + request2.getRequestLine() + "->" + ex);
84 }
85
86 public void cancelled() {
87 latch1.countDown();
88 System.out.println("cancelled: " + request2.getRequestLine() + " cancelled");
89 }
90 });
91 System.out.println("#2 await...");
92 latch1.await();
93 System.out.println("#2 done.");
94
95 // In real world one most likely would also want to stream
96 // request and response body content
97 final CountDownLatch latch2 = new CountDownLatch(1);
98 final HttpPost request3 = new HttpPost(url);
99 ArrayList<NameValuePair> params3;
100 params3 = new ArrayList<NameValuePair>();
101 params3.add(new BasicNameValuePair("p31", "v31"));
102 params3.add(new BasicNameValuePair("p32", "v32"));
103 request3.setEntity(new UrlEncodedFormEntity(params3, "UTF-8"));
104 HttpAsyncRequestProducer producer3 = HttpAsyncMethods.create(request3);
105 AsyncCharConsumer<HttpResponse> consumer3 = new AsyncCharConsumer<HttpResponse>() {
106
107 HttpResponse response;
108
109 @Override
110 protected void onResponseReceived(final HttpResponse response) {
111 this.response = response;
112 System.out.println("onResponseReceived:");
113 }
114
115 @Override
116 protected void onCharReceived(final CharBuffer buf, final IOControl ioctrl) throws IOException {
117 // Do something useful
118 System.out.println("onCharReceived:");
119 }
120
121 @Override
122 protected void releaseResources() {
123 System.out.println("releaseResources:");
124 }
125
126 @Override
127 protected HttpResponse buildResult(final HttpContext context) {
128 System.out.println("buildResult:");
129 return this.response;
130 }
131 };
132
133 System.out.println("#3 started...");
134 httpclient.execute(producer3, consumer3, new FutureCallback<HttpResponse>() {
135
136 public void completed(final HttpResponse response3) {
137 latch2.countDown();
138 System.out.println(request3.getRequestLine() + "->" + response3.getStatusLine());
139 }
140
141 public void failed(final Exception ex) {
142 latch2.countDown();
143 System.out.println(request3.getRequestLine() + "->" + ex);
144 }
145
146 public void cancelled() {
147 latch2.countDown();
148 System.out.println(request3.getRequestLine() + " cancelled");
149 }
150 });
151 System.out.println("#3 await...");
152 latch2.await();
153 System.out.println("#3 done.");
154
155 } finally {
156 httpclient.close();
157 }
158 }
159}
これを実行すると、サーバー側では以下のようなPOSTメソッドなHTTPリクエストを受信します。(実際には3つリクエストしますが、1つだけ示したものです)
terminal
1$ nc -l 8000
2POST /cgi-bin/t1.py HTTP/1.1
3Content-Length: 15
4Content-Type: application/x-www-form-urlencoded; charset=UTF-8
5Host: localhost:8000
6Connection: Keep-Alive
7User-Agent: Apache-HttpAsyncClient/4.1.4 (Java/11.0.9)
8
9p11=v11&p12=v12
Windows10上で、Java SE11(11.0.9)で実行しました。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。