HttpClientUtils.java
9.45 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package com.sincere.common.util;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
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.util.EntityUtils;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import static jdk.nashorn.internal.runtime.ECMAErrors.getMessage;
/**
* HttpClient4.3工具类
* @author chen
* @version 1.0
* @date 2019/10/11 0011 10:17
*/
public class HttpClientUtils {
private static RequestConfig requestConfig = null;
static {
// 设置请求和传输超时时间
requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).build();
}
public static void main(String[] args){
// String url = "http://http://zhktest.114school.com.cn/szkjapi/toyxy/addTeacherOrg" ;
// String json = "{\"name\": \"子部门\",\"groupname\": \"父部门\",\"schoolid\": \"16\",\"token\": \"05719991\"}" ;
// JSONObject jsonObject = HttpClientUtils.httpPostJson(url,json);
// System.out.println(jsonObject.toJSONString());
String url = "http://campus.myjxt.com/api/Room/GetListPageRoom" ;
String json = "PageIndex=1&roomId=6583&pageSize=9999" ;
JSONObject jsonObject = HttpClientUtils.httpPost(url,json);
System.out.println(jsonObject.toJSONString());
}
/**
* post请求传输json参数
* @param url url地址
* @param jsonParam 参数
* @return
*/
public static JSONObject httpPostJson(String url, String jsonParam) {
// post请求返回结果
CloseableHttpClient httpClient = HttpClients.createDefault();
JSONObject jsonResult = null;
HttpPost httpPost = new HttpPost(url);
// 设置请求和传输超时时间
httpPost.setConfig(requestConfig);
try {
if (null != jsonParam) {
// 解决中文乱码问题
StringEntity entity = new StringEntity(jsonParam, "utf-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
}
CloseableHttpResponse result = httpClient.execute(httpPost);
// 请求发送成功,并得到响应
if (result.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String str = "";
// 读取服务器返回过来的json字符串数据
str = EntityUtils.toString(result.getEntity(), "utf-8");
// 把json字符串转换成json对象
jsonResult = JSONObject.parseObject(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
httpPost.releaseConnection();
}
return jsonResult;
}
/**
* post请求传输String参数 例如:name=Jack&sex=1&type=2
* Content-type:application/x-www-form-urlencoded
* @param url url地址
* @param strParam 参数
* @return
*/
public static JSONObject httpPost(String url, String strParam) {
// post请求返回结果
CloseableHttpClient httpClient = HttpClients.createDefault();
JSONObject jsonResult = null;
HttpPost httpPost = new HttpPost(url);
httpPost.setConfig(requestConfig);
try {
if (null != strParam) {
// 解决中文乱码问题
StringEntity entity = new StringEntity(strParam, "utf-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/x-www-form-urlencoded");
httpPost.setEntity(entity);
}
CloseableHttpResponse result = httpClient.execute(httpPost);
// 请求发送成功,并得到响应
if (result.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String str = "";
// 读取服务器返回过来的json字符串数据
str = EntityUtils.toString(result.getEntity(), "utf-8");
// 把json字符串转换成json对象
jsonResult = JSONObject.parseObject(str);
}
} catch (IOException e) {
} finally {
httpPost.releaseConnection();
}
return jsonResult;
}
/**
* 发送get请求
* @param url 路径
* @return
*/
public static JSONObject httpGet(String url) {
// get请求返回结果
JSONObject jsonResult = null;
CloseableHttpClient client = HttpClients.createDefault();
// 发送get请求
HttpGet request = new HttpGet(url);
request.setConfig(requestConfig);
try {
CloseableHttpResponse response = client.execute(request);
// 请求发送成功,并得到响应
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 读取服务器返回过来的json字符串数据
HttpEntity entity = response.getEntity();
String strResult = EntityUtils.toString(entity, "utf-8");
// 把json字符串转换成json对象
jsonResult = JSONObject.parseObject(strResult);
}
} catch (IOException e) {
} finally {
request.releaseConnection();
}
return jsonResult;
}
public static String httpGet2(String url) {
// get请求返回结果
JSONObject jsonResult = null;
String result = "" ;
CloseableHttpClient client = HttpClients.createDefault();
// 发送get请求
HttpGet request = new HttpGet(url);
request.setConfig(requestConfig);
try {
CloseableHttpResponse response = client.execute(request);
// 请求发送成功,并得到响应
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 读取服务器返回过来的json字符串数据
HttpEntity entity = response.getEntity();
String strResult = EntityUtils.toString(entity, "utf-8");
// 把json字符串转换成json对象
//jsonResult = JSONObject.parseObject(strResult);
result = strResult;
}
} catch (IOException e) {
} finally {
request.releaseConnection();
}
return result;
}
public static InputStream GetFileInputStream(String fileUrl){
try{
URL url = new URL(fileUrl);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//设置超时间为3秒
conn.setConnectTimeout(8*1000);
//得到输入流
InputStream inputStream = conn.getInputStream();
return inputStream ;
}catch (Exception e){
}
return null ;
}
/**
*
* @param wsdl
* @param ns
* @param method
* @param list 参数
* @return
*/
public synchronized static String invoiceWebService(String wsdl, String ns, String method, List<String> list){
StringBuffer stringBuffer = new StringBuffer();
//拼接参数
for (int i = 0; i < list.size(); i++) {
stringBuffer.append("<arg" + i + ">" + list.get(i) + "</arg" + i + ">");
}
//拼接SOAP
StringBuffer soapRequestData = new StringBuffer("");
soapRequestData.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://service.info.employee.staffing.talentbase.neusoft.com/\">");
soapRequestData.append("<soapenv:Header/>");
soapRequestData.append("<soapenv:Body>");
soapRequestData.append("<ser:" + method + ">");
soapRequestData.append(stringBuffer);
soapRequestData.append("</ser:" + method + ">");
soapRequestData.append("</soapenv:Body>" + "</soapenv:Envelope>");
PostMethod postMethod = new PostMethod(wsdl);
// 然后把Soap请求数据添加到PostMethod中
byte[] b = null;
InputStream is = null;
try {
b = soapRequestData.toString().getBytes("utf-8");
is = new ByteArrayInputStream(b, 0, b.length);
RequestEntity re = new InputStreamRequestEntity(is, b.length, "text/xml; charset=UTF-8");
postMethod.setRequestEntity(re);
HttpClient httpClient = new HttpClient();
int status = httpClient.executeMethod(postMethod);
if (status == 200) {
return postMethod.getResponseBodyAsString() ;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try{
if (is != null) {
is.close();
}
}catch (Exception e){
}
}
return null;
}
}