Skip to content

Commit 4dee6b0

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

3 files changed

Lines changed: 136 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.wdbyte.httpclient;
2+
3+
import java.io.IOException;
4+
5+
import org.apache.hc.client5.http.classic.methods.HttpGet;
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.HttpEntity;
10+
import org.apache.hc.core5.http.ParseException;
11+
import org.apache.hc.core5.http.io.entity.EntityUtils;
12+
13+
/**
14+
* @author https://www.wdbyte.com
15+
* @date 2022/04/01
16+
*/
17+
public class HttpClient5Get {
18+
19+
public static void main(String[] args) {
20+
String result = get("http://httpbin.org/get");
21+
System.out.println(result);
22+
}
23+
24+
public static String get(String url) {
25+
String resultContent = null;
26+
HttpGet httpGet = new HttpGet(url);
27+
try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
28+
try (CloseableHttpResponse response = httpclient.execute(httpGet)) {
29+
// 获取状态码
30+
System.out.println(response.getVersion()); // HTTP/1.1
31+
System.out.println(response.getCode()); // 200
32+
System.out.println(response.getReasonPhrase()); // OK
33+
HttpEntity entity = response.getEntity();
34+
// 获取响应信息
35+
resultContent = EntityUtils.toString(entity);
36+
}
37+
} catch (IOException | ParseException e) {
38+
e.printStackTrace();
39+
}
40+
return resultContent;
41+
}
42+
43+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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.client5.http.fluent.Response;
7+
8+
/**
9+
* @author https://www.wdbyte.com
10+
*/
11+
public class HttpClient5GetFluent {
12+
13+
public static void main(String[] args) {
14+
System.out.println(get("http://httpbin.org/get"));
15+
}
16+
17+
public static String get(String url) {
18+
String result = null;
19+
try {
20+
Response response = Request.get(url).execute();
21+
result = response.returnContent().asString();
22+
} catch (IOException e) {
23+
e.printStackTrace();
24+
}
25+
return result;
26+
}
27+
28+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.wdbyte.httpclient;
2+
3+
import java.io.IOException;
4+
import java.net.URI;
5+
import java.net.URISyntaxException;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
import org.apache.hc.client5.http.classic.methods.HttpGet;
10+
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
11+
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
12+
import org.apache.hc.client5.http.impl.classic.HttpClients;
13+
import org.apache.hc.core5.http.HttpEntity;
14+
import org.apache.hc.core5.http.NameValuePair;
15+
import org.apache.hc.core5.http.ParseException;
16+
import org.apache.hc.core5.http.io.entity.EntityUtils;
17+
import org.apache.hc.core5.http.message.BasicNameValuePair;
18+
import org.apache.hc.core5.net.URIBuilder;
19+
20+
/**
21+
* @author https://www.wdbyte.com
22+
* @date 2022/04/01
23+
*/
24+
public class HttpClient5GetParams {
25+
26+
public static void main(String[] args) {
27+
String result = get("http://httpbin.org/get");
28+
System.out.println(result);
29+
}
30+
31+
public static String get(String url) {
32+
String resultContent = null;
33+
HttpGet httpGet = new HttpGet(url);
34+
// 表单参数
35+
List<NameValuePair> nvps = new ArrayList<>();
36+
// GET 请求参数
37+
nvps.add(new BasicNameValuePair("username", "wdbyte.com"));
38+
nvps.add(new BasicNameValuePair("password", "secret"));
39+
// 增加到请求 URL 中
40+
try {
41+
URI uri = new URIBuilder(new URI(url))
42+
.addParameters(nvps)
43+
.build();
44+
httpGet.setUri(uri);
45+
} catch (URISyntaxException e) {
46+
throw new RuntimeException(e);
47+
}
48+
49+
try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
50+
try (CloseableHttpResponse response = httpclient.execute(httpGet)) {
51+
// 获取状态码
52+
System.out.println(response.getVersion()); // HTTP/1.1
53+
System.out.println(response.getCode()); // 200
54+
System.out.println(response.getReasonPhrase()); // OK
55+
HttpEntity entity = response.getEntity();
56+
// 获取响应信息
57+
resultContent = EntityUtils.toString(entity);
58+
}
59+
} catch (IOException | ParseException e) {
60+
e.printStackTrace();
61+
}
62+
return resultContent;
63+
}
64+
65+
}

0 commit comments

Comments
 (0)