HttpInvokerUtil.java
4.44 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
package com.rnt.utils;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
/**
* HTTP请求类
* @author LiHong
*/
public class HttpInvokerUtil {
/**
* GET请求
* @param getUrl
* @throws IOException
* @return 提取HTTP响应报文包体,以字符串形式返回
*/
public static String httpGet(String getUrl,Map<String, String> getHeaders) throws IOException {
URL getURL = new URL(getUrl);
HttpURLConnection connection = (HttpURLConnection) getURL.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)");
if(getHeaders != null) {
for(String pKey : getHeaders.keySet()) {
connection.setRequestProperty(pKey, getHeaders.get(pKey));
}
}
connection.connect();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder sbStr = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sbStr.append(line);
}
bufferedReader.close();
connection.disconnect();
return new String(sbStr.toString().getBytes(),"utf-8");
}
/**
* POST请求
* @param postUrl
* @param postHeaders
* @param postEntity
* @throws IOException
* @return 提取HTTP响应报文包体,以字符串形式返回
*/
public static String httpPost(String postUrl,Map<String, String> postHeaders, String postEntity) throws IOException {
URL postURL = new URL(postUrl);
HttpURLConnection httpURLConnection = (HttpURLConnection) postURL.openConnection();
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setUseCaches(false);
httpURLConnection.setInstanceFollowRedirects(true);
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
if(postHeaders != null) {
for(String pKey : postHeaders.keySet()) {
httpURLConnection.setRequestProperty(pKey, postHeaders.get(pKey));
}
}
if(postEntity != null) {
DataOutputStream out = new DataOutputStream(httpURLConnection.getOutputStream());
out.writeBytes(postEntity);
out.flush();
out.close(); // flush and close
}
//connection.connect();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
StringBuilder sbStr = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sbStr.append(line);
}
bufferedReader.close();
httpURLConnection.disconnect();
return new String(sbStr.toString().getBytes(),"utf-8");
}
/**
* POST请求 ,解决中文乱码问题
* @param postUrl
* @param postHeaders
* @param postEntity
* @throws IOException
* @return 提取HTTP响应报文包体,以字符串形式返回
*/
public static String httpPost1(String postUrl,Map<String, String> postHeaders, String postEntity) throws IOException {
URL postURL = new URL(postUrl);
HttpURLConnection httpURLConnection = (HttpURLConnection) postURL.openConnection();
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setUseCaches(false);
httpURLConnection.setInstanceFollowRedirects(true);
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
StringBuilder sbStr = new StringBuilder();
if(postHeaders != null) {
for(String pKey : postHeaders.keySet()) {
httpURLConnection.setRequestProperty(pKey, postHeaders.get(pKey));
}
}
if(postEntity != null) {
PrintWriter out = new PrintWriter(new OutputStreamWriter(httpURLConnection.getOutputStream(),"utf-8"));
out.println(postEntity);
out.close();
BufferedReader in = new BufferedReader(new InputStreamReader(httpURLConnection
.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
sbStr.append(inputLine);
}
in.close();
}
httpURLConnection.disconnect();
return new String(sbStr.toString().getBytes(),"utf-8");
}
}