HttpClientUtils.java
7.83 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package com.cnlive.shenhe.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class HttpClientUtils {
public static String post(String url, Map<String, Object> map) {
// 创建post请求
HttpPost post = new HttpPost(url);
String text = "请求失败";
try {
// 创建http访问的客户端
HttpClient client = HttpClients.createDefault();
List<NameValuePair> param = new ArrayList<>();
if(map!=null&&map.size()>0){
for (Map.Entry<String, Object> entry : map.entrySet()) {
param.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
}
}
// 用参数构造请求实体
HttpEntity postEntity = new UrlEncodedFormEntity(param, "utf-8");
// 把请求实体放到post请求中
post.setEntity(postEntity);
HttpResponse response = client.execute(post); // 执行post请求
HttpEntity entity = response.getEntity();// 从响应中取得一个响应实体
text = EntityUtils.toString(entity);// 把响应实体转成文本信息
} catch (Exception e) {
e.printStackTrace();
} finally {
post.releaseConnection();// 释放连接
}
return text;
}
public static String get(String url) {
String text = "请求失败";
HttpGet httpGet = null;
try {
HttpClient client = HttpClients.createDefault();// 创建http访问的客户端
httpGet = new HttpGet(url);
HttpResponse response = client.execute(httpGet); // 执行get请求
HttpEntity entity = response.getEntity();// 从响应中取得一个响应实体
text = EntityUtils.toString(entity);// 把响应实体转成文本信息
} catch (Exception e) {
e.printStackTrace();
} finally {
httpGet.releaseConnection();// 释放连接
}
return text;
}
public static String get(String url, Map<String, String> map) {
String text = "请求失败";
HttpGet httpGet = null;
try {
HttpClient client = HttpClients.createDefault();// 创建http访问的客户端
List<NameValuePair> params = new ArrayList<NameValuePair>();
if (map != null && map.size() > 0) {
for (Map.Entry<String, String> entry : map.entrySet()) {
params.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
}
String paramsStr = EntityUtils.toString(new UrlEncodedFormEntity(params, "UTF-8"));
httpGet = new HttpGet(url + "?" + paramsStr);
HttpResponse response = client.execute(httpGet); // 执行get请求
HttpEntity entity = response.getEntity();// 从响应中取得一个响应实体
text = EntityUtils.toString(entity);// 把响应实体转成文本信息
} catch (Exception e) {
e.printStackTrace();
} finally {
httpGet.releaseConnection();// 释放连接
}
return text;
}
public static String post_str(String url, Map<String, String> map) {
HttpPost post = null;
String text = "请求失败";
try {
HttpClient client = HttpClients.createDefault();// 创建http访问的客户端
List<NameValuePair> param = new ArrayList<>();
if (map != null && map.size() > 0) {
for (Map.Entry<String, String> entry : map.entrySet()) {
param.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
}
HttpEntity postEntity = new UrlEncodedFormEntity(param, "utf-8");// 用参数构造请求实体
post = new HttpPost(url);// 创建post请求
post.setEntity(postEntity);// 把请求实体放到post请求中
HttpResponse response = client.execute(post); // 执行post请求
HttpEntity entity = response.getEntity();// 从响应中取得一个响应实体
text = EntityUtils.toString(entity);// 把响应实体转成文本信息
} catch (Exception e) {
e.printStackTrace();
} finally {
post.releaseConnection();// 释放连接
}
return text;
}
public static String HttpPostWithJson(String url, String json) {
CloseableHttpClient httpClient = null;
try {
httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-Type", "application/json;charset=utf-8");
StringEntity se = new StringEntity(json, "UTF-8");
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
"application/json"));
httpPost.setEntity(se);
CloseableHttpResponse response = httpClient.execute(httpPost);
// 返回数据
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity, "UTF-8");
return result;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (httpClient != null) {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* httpPost
* @param url 请求url
* @param jsonParam
* @param noNeedResponse 是否获取接口response
* @return
* @throws Exception
*/
public static JSONObject httpPost(String url, JSONObject jsonParam, boolean noNeedResponse) throws Exception{
CloseableHttpClient httpClient = HttpClients.createDefault();
JSONObject jsonResult = null;
HttpPost method = new HttpPost(url);
try {
if (null != jsonParam) {
StringEntity entity = new StringEntity(jsonParam.toString(), "UTF-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
method.setEntity(entity);
}
HttpResponse result = httpClient.execute(method);
url = URLDecoder.decode(url, "UTF-8");
if (result.getStatusLine().getStatusCode() == 200) {
String str = "";
try {
str = EntityUtils.toString(result.getEntity());
if (noNeedResponse) {
return null;
}
jsonResult = JSON.parseObject(str);
} catch (Exception e) {
System.out.println("posturl:" + url + ", err=" + e.getMessage());
throw new Exception(e);
}
}
} catch (IOException e) {
System.out.println("posturl:" + url + ", err=" + e.getMessage());
throw new IOException(e);
}
return jsonResult;
}
}