-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathHTTPClient-POST.java
More file actions
89 lines (86 loc) · 3.16 KB
/
HTTPClient-POST.java
File metadata and controls
89 lines (86 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
---------------------
常规POST请求 |
---------------------
public static void main(String[] args) throws Exception {
// 创建Httpclient对象 打开浏览器
CloseableHttpClient httpclient = HttpClients.createDefault();
// 创建http POST请求 输入地址
HttpPost httpPost = new HttpPost("http://www.oschina.net/");
CloseableHttpResponse response = null;
try {
// 执行请求
response = httpclient.execute(httpPost);
// 判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String content = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println(content);
}
} finally {
if (response != null) {
response.close();
}
httpclient.close();
}
}
---------------------
带参POST请求 |
---------------------
public static void main(String[] args) throws Exception {
// 创建Httpclient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
// 创建http POST请求
HttpPost httpPost = new HttpPost("http://www.oschina.net/search");
// 设置2个post参数,一个是scope、一个是q
List<NameValuePair> parameters = new ArrayList<NameValuePair>(0);
parameters.add(new BasicNameValuePair("scope", "project"));
parameters.add(new BasicNameValuePair("q", "java"));
// 构造一个form表单式的实体
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters,"utf-8");
// 将请求实体设置到httpPost对象中
httpPost.setEntity(formEntity);
CloseableHttpResponse response = null;
try {
// 执行请求
response = httpclient.execute(httpPost);
// 判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String content = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println(content);
}
} finally {
if (response != null) {
response.close();
}
httpclient.close();
}
}
---------------------
带参JSON的POST请求 |
---------------------
public HttpResult doPostJson(String url,String json) throws ClientProtocolException, IOException{
// 创建http POST请求
HttpPost httpPost = new HttpPost(url);
httpPost.setConfig(this.config);
if(json != null){
/**
JSON的本质就是字符串,所以这里使用: StringEntity
*/
StringEntity entity = new StringEntity(json,"utf-8");
//设置请求头JSON
entity.setContentType("application/json");
httpPost.setEntity(entity);
}
CloseableHttpResponse response = null;
try {
// 执行请求
response = httpClient.execute(httpPost);
HttpResult result = new HttpResult(
response.getStatusLine().getStatusCode(),
EntityUtils.toString(response.getEntity(),"utf-8"));
return result;
} finally {
if (response != null) {
response.close();
}
}
}