Skip to content

Commit f0d82dc

Browse files
committed
feat: 增加 HttpClient5 POST 请求(https://www.wdbyte.com/httpclient5/post.html)
1 parent 4dee6b0 commit f0d82dc

3 files changed

Lines changed: 178 additions & 0 deletions

File tree

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.wdbyte.httpclient;
2+
3+
import java.io.IOException;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
import org.apache.hc.client5.http.classic.methods.HttpPost;
8+
import org.apache.hc.client5.http.entity.UrlEncodedFormEntity;
9+
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
10+
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
11+
import org.apache.hc.client5.http.impl.classic.HttpClients;
12+
import org.apache.hc.core5.http.HttpEntity;
13+
import org.apache.hc.core5.http.NameValuePair;
14+
import org.apache.hc.core5.http.ParseException;
15+
import org.apache.hc.core5.http.io.entity.EntityUtils;
16+
import org.apache.hc.core5.http.message.BasicNameValuePair;
17+
18+
/**
19+
* @author https://www.wdbyte.com
20+
*/
21+
public class HttpClient5Post {
22+
23+
public static void main(String[] args) {
24+
String result = post("http://httpbin.org/post");
25+
System.out.println(result);
26+
}
27+
28+
/**
29+
* {
30+
* "args": {},
31+
* "data": "",
32+
* "files": {},
33+
* "form": {
34+
* "password": "secret",
35+
* "username": "wdbyte.com"
36+
* },
37+
* "headers": {
38+
* "Accept-Encoding": "gzip, x-gzip, deflate",
39+
* "Content-Length": "31",
40+
* "Content-Type": "application/x-www-form-urlencoded; charset=ISO-8859-1",
41+
* "Host": "httpbin.org",
42+
* "User-Agent": "Apache-HttpClient/5.1.3 (Java/17)",
43+
* "X-Amzn-Trace-Id": "Root=1-62ab2708-4425003d3641a0a75cfeda74"
44+
* },
45+
* "json": null,
46+
* "origin": "47.251.4.198",
47+
* "url": "http://httpbin.org/post"
48+
* }
49+
*/
50+
public static String post(String url) {
51+
String result = null;
52+
HttpPost httpPost = new HttpPost(url);
53+
// 表单参数
54+
List<NameValuePair> nvps = new ArrayList<>();
55+
// POST 请求参数
56+
nvps.add(new BasicNameValuePair("username", "wdbyte.com"));
57+
nvps.add(new BasicNameValuePair("password", "secret"));
58+
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
59+
try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
60+
try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
61+
System.out.println(response.getVersion()); // HTTP/1.1
62+
System.out.println(response.getCode()); // 200
63+
System.out.println(response.getReasonPhrase()); // OK
64+
65+
HttpEntity entity = response.getEntity();
66+
// 获取响应信息
67+
result = EntityUtils.toString(entity);
68+
// 确保流被完全消费
69+
EntityUtils.consume(entity);
70+
}
71+
} catch (IOException | ParseException e) {
72+
e.printStackTrace();
73+
}
74+
return result;
75+
}
76+
77+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.wdbyte.httpclient;
2+
3+
import java.io.IOException;
4+
5+
import org.apache.hc.client5.http.fluent.Request;
6+
import org.apache.hc.core5.http.message.BasicNameValuePair;
7+
8+
/**
9+
* @author https://www.wdbyte.com
10+
*/
11+
public class HttpClient5PostFluent {
12+
13+
public static void main(String[] args) {
14+
String result = post("http://httpbin.org/post");
15+
System.out.println(result);
16+
}
17+
18+
public static String post(String url) {
19+
String result = null;
20+
Request request = Request.post(url);
21+
// POST 请求参数
22+
request.bodyForm(
23+
new BasicNameValuePair("username", "wdbyte.com"),
24+
new BasicNameValuePair("password", "secret"));
25+
try {
26+
result = request.execute().returnContent().asString();
27+
} catch (IOException e) {
28+
e.printStackTrace();
29+
}
30+
return result;
31+
}
32+
33+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.wdbyte.httpclient;
2+
3+
import java.io.IOException;
4+
5+
import org.apache.hc.client5.http.classic.methods.HttpPost;
6+
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
7+
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
8+
import org.apache.hc.client5.http.impl.classic.HttpClients;
9+
import org.apache.hc.core5.http.ContentType;
10+
import org.apache.hc.core5.http.ParseException;
11+
import org.apache.hc.core5.http.io.entity.EntityUtils;
12+
import org.apache.hc.core5.http.io.entity.StringEntity;
13+
14+
/**
15+
* @author https://www.wdbyte.com
16+
*/
17+
public class HttpClient5PostWithJson {
18+
19+
/**
20+
* {
21+
* "args": {},
22+
* "data": "{ \"password\": \"secret\", \"username\": \"wdbyte.com\"}",
23+
* "files": {},
24+
* "form": {},
25+
* "headers": {
26+
* "Accept-Encoding": "gzip, x-gzip, deflate",
27+
* "Content-Length": "55",
28+
* "Content-Type": "text/plain; charset=ISO-8859-1",
29+
* "Host": "httpbin.org",
30+
* "User-Agent": "Apache-HttpClient/5.1.3 (Java/17)",
31+
* "X-Amzn-Trace-Id": "Root=1-62b6ab18-2aec3a5731e325620f1f5717"
32+
* },
33+
* "json": {
34+
* "password": "secret",
35+
* "username": "wdbyte.com"
36+
* },
37+
* "origin": "42.120.74.8",
38+
* "url": "http://httpbin.org/post"
39+
* }
40+
*
41+
* @param args
42+
*/
43+
public static void main(String[] args) {
44+
String json = "{"
45+
+ " \"password\": \"secret\","
46+
+ " \"username\": \"wdbyte.com\""
47+
+ "}";
48+
String result = post("http://httpbin.org/post", json);
49+
System.out.println(result);
50+
}
51+
52+
public static String post(String url, String jsonBody) {
53+
String result = null;
54+
HttpPost httpPost = new HttpPost(url);
55+
httpPost.setEntity(new StringEntity(jsonBody, ContentType.APPLICATION_JSON));
56+
57+
try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
58+
try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
59+
// 获取响应信息
60+
result = EntityUtils.toString(response.getEntity());
61+
}
62+
} catch (IOException | ParseException e) {
63+
e.printStackTrace();
64+
}
65+
return result;
66+
}
67+
68+
}

0 commit comments

Comments
 (0)