HttpRequest.java
12.9 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package tools;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.aspectj.weaver.ast.Call;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class HttpRequest {
/**
* 向指定URL发送GET方法的请求
*
* @param url
* 发送请求的URL
* @param param
* 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return URL 所代表远程资源的响应结果
* @throws Exception
* @throws MalformedURLException
*/
/*
* public static String queryWebservice2(String url) throws
* MalformedURLException, Exception{ String xml = ""+
* "<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
* "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
* + "<soap:Body>"+
* "<GetShipmentStatusByBillId xmlns=\"http://ShipmentStatus\">"+
* "<Freight>172-20966794</Freight>"+ "<Xquery>BA8EC</Xquery>"+
* "</GetShipmentStatusByBillId>"+ "</soap:Body>"+ "</soap:Envelope>";
* Client c = new Client(new URL(url)); Object[] o =
* c.invoke("GetShipmentStatusByBillId", new Object[]{xml}); if(o != null)
* return o[0].toString(); return null; }
*/
public static String queryWebservice(String url) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
System.out.println("start!");
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setDoInput(true);
httpConn.setDoOutput(true);
httpConn.setRequestMethod("POST");
httpConn.setRequestProperty("Host", "10.50.3.66");
// httpConn.setRequestProperty("Content-Type",
// "application/soap+xml; charset=utf-8");
httpConn.setRequestProperty("Connect-Type", "text/xml;charset=UTF-8");
// httpConn.setRequestProperty("SOAPAction",
// "http://ShipmentStatus/GetShipmentStatusByBillId");
String data = "" + "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
+ "<soap:Body>" + "<GetShipmentStatusByBillId xmlns=\"http://ShipmentStatus\">"
+ "<Freight>172-20966794</Freight>" + "<Xquery>BA8EC</Xquery>" + "</GetShipmentStatusByBillId>"
+ "</soap:Body>" + "</soap:Envelope>";
httpConn.setRequestProperty("Content-Length", data.length() + "");
OutputStream out1 = httpConn.getOutputStream();
out1.write(data.getBytes());
System.out.println("response code:" + httpConn.getResponseCode());
if (httpConn.getResponseCode() == 200) {
// 获得输入流
InputStream in1 = httpConn.getInputStream();
// 使用输入流的缓冲区
BufferedReader reader = new BufferedReader(new InputStreamReader(in1));
StringBuffer sb = new StringBuffer();
String line = null;
// 读取输入流
while ((line = reader.readLine()) != null) {
sb.append(line);
}
/*
* //创建sax的读取器 SAXReader saxReader = new SAXReader(); //创建文档对象
* Document doc = saxReader.read(new
* StringReader(sb.toString())); //获得请求响应return元素 List<Element>
* eles = doc.selectNodes("//return"); for(Element ele : eles){
* System.out.println(ele.getText()); }
*/
return sb.toString();
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("end");
return null;
}
public static String sendGet(String url, String param) {
String result = "";
BufferedReader in = null;
try {
String urlNameString = url;// + "?" + param;
URL realUrl = new URL(urlNameString);
// 打开和URL之间的连接
URLConnection connection = realUrl.openConnection();
// 设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 建立实际的连接
connection.connect();
// 获取所有响应头字段
Map<String, List<String>> map = connection.getHeaderFields();
// 遍历所有的响应头字段
for (String key : map.keySet()) {
System.out.println(key + "--->" + map.get(key));
}
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送GET请求出现异常!" + e);
e.printStackTrace();
}
// 使用finally块来关闭输入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}
public static String sendPost2(String url, String Params) {
OutputStreamWriter out = null;
BufferedReader reader = null;
String response = "";
try {
URL httpUrl = null; // HTTP URL类 用这个类来创建连接
// 创建URL
httpUrl = new URL(url);
// 建立连接
HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("connection", "keep-alive");
conn.setUseCaches(false);// 设置不要缓存
conn.setInstanceFollowRedirects(true);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.connect();
// POST请求
out = new OutputStreamWriter(conn.getOutputStream());
out.write(Params);
out.flush();
// 读取响应
reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String lines;
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes(), "utf-8");
response += lines;
}
reader.close();
// 断开连接
conn.disconnect();
} catch (Exception e) {
System.out.println("发送 POST 请求出现异常!" + e);
e.printStackTrace();
}
// 使用finally块来关闭输出流、输入流
finally {
try {
if (out != null) {
out.close();
}
if (reader != null) {
reader.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return response;
}
/**
* 向指定 URL 发送POST方法的请求
*
* @param url
* 发送请求的 URL
* @param param
* 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return 所代表远程资源的响应结果
*/
public static String sendPost(String url, String param) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("Origin", "chrome-extension://zejoelaoggembcahzgimdiliamlcdmfm");
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Accept", "*/*");
conn.setRequestProperty("Accept-Encoding", "gzip,deflate,utf-8");
conn.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.9");
//conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//conn.setRequestProperty("Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
//conn.setRequestProperty("Content-Encoding", "gzip");
//conn.setRequestProperty("Content-Type", "text/html;charset=UTF-8");
//conn.setRequestProperty("accept", "*/*");
//conn.setRequestProperty("connection", "Keep-Alive");
//conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// out = new OutputStreamWriter(conn.getOutputStream());
// 发送请求参数
out.print(param);
// out.write(param);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
// in = conn.getInputStream();
String line;
/*byte buffer[] = new byte[1024];
while(in.read(buffer) > 0)
{
result += new String(buffer, "UTF-8");
}*/
while ((line = in.readLine()) != null) {
//System.out.println("hello");
result += line;
}
} catch (Exception e) {
System.out.println("发送 POST 请求出现异常!" + e);
e.printStackTrace();
}
// 使用finally块来关闭输出流、输入流
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}
public static String sendPost3(String url,String Params){
OutputStreamWriter out = null;
BufferedReader reader = null;
String response="";
try {
URL httpUrl = null; //HTTP URL类 用这个类来创建连接
//创建URL
httpUrl = new URL(url);
//建立连接
HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("connection", "keep-alive");
conn.setUseCaches(false);//设置不要缓存
conn.setInstanceFollowRedirects(true);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.connect();
//POST请求
out = new OutputStreamWriter(
conn.getOutputStream());
out.write(Params);
out.flush();
//读取响应
reader = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String lines;
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes(), "utf-8");
response+=lines;
}
reader.close();
// 断开连接
conn.disconnect();
} catch (Exception e) {
System.out.println("发送 POST 请求出现异常!"+e);
e.printStackTrace();
}
//使用finally块来关闭输出流、输入流
finally{
try{
if(out!=null){
out.close();
}
if(reader!=null){
reader.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
return response;
}
public static String doHttpPost(String URL, String xmlInfo) {
byte[] xmlData = xmlInfo.getBytes();
InputStream instr = null;
java.io.ByteArrayOutputStream out = null;
try {
URL url = new URL(URL);
URLConnection urlCon = url.openConnection();
urlCon.setDoOutput(true);
urlCon.setDoInput(true);
urlCon.setUseCaches(false);
urlCon.setRequestProperty("content-Type", "application/json");
urlCon.setRequestProperty("charset", "utf-8");
urlCon.setRequestProperty("Content-length",
String.valueOf(xmlData.length));
System.out.println(String.valueOf(xmlData.length));
DataOutputStream printout = new DataOutputStream(
urlCon.getOutputStream());
printout.write(xmlData);
printout.flush();
printout.close();
instr = urlCon.getInputStream();
byte[] bis = IOUtils.toByteArray(instr);
String ResponseString = new String(bis, "UTF-8");
if ((ResponseString == null) || ("".equals(ResponseString.trim()))) {
System.out.println("返回空");
}
System.out.println("返回数据为:" + ResponseString);
return ResponseString;
} catch (Exception e) {
e.printStackTrace();
return "0";
} finally {
try {
out.close();
instr.close();
} catch (Exception ex) {
return "0";
}
}
}
}