client.HttpURLConnectionClient.java
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
|
package
client;
import
java.io.BufferedReader;
import
java.io.InputStream;
import
java.io.InputStreamReader;
import
java.io.OutputStream;
import
java.net.HttpURLConnection;
import
java.net.URL;
import
java.util.Map;
import
java.util.Map.Entry;
import
javax.net.ssl.HttpsURLConnection;
import
javax.net.ssl.SSLContext;
import
javax.net.ssl.SSLSocketFactory;
import
javax.net.ssl.TrustManager;
import
org.apache.log4j.Logger;
public
class
HttpURLConnectionClient {
private
final
String POST=
"POST"
;
private
final
String GET=
"GET"
;
private
final
String UTF_8 =
"UTF-8"
;
private
static
HttpURLConnectionClient httpURLConnectionClient =
new
HttpURLConnectionClient();
private
HttpURLConnectionClient(){}
public
static
HttpURLConnectionClient getInstance(){
return
httpURLConnectionClient;
}
private
Logger logger = Logger.getLogger(
this
.getClass());
public
String get(String requestUrl) {
StringBuffer buffer =
new
StringBuffer();
BufferedReader bufferedReader =
null
;
OutputStream outputStream =
null
;
HttpURLConnection httpUrlConn =
null
;
try
{
httpUrlConn = createHttpURLConnection(requestUrl);
httpUrlConn.setRequestMethod(GET);
// 将返回的输入流转换成字符串
InputStream inputStream = httpUrlConn.getInputStream();
InputStreamReader inputStreamReader =
new
InputStreamReader(inputStream, UTF_8);
bufferedReader =
new
BufferedReader(inputStreamReader);
String str =
null
;
while
((str = bufferedReader.readLine()) !=
null
) {
buffer.append(str);
}
return
buffer.toString();
}
catch
(Exception e) {
throw
new
RuntimeException(e);
}
finally
{
close(bufferedReader,outputStream,httpUrlConn);
}
}
public
String post(String requestUrl,Map<String,String> headers, String data) {
StringBuffer buffer =
new
StringBuffer();
BufferedReader bufferedReader =
null
;
OutputStream outputStream =
null
;
HttpURLConnection httpUrlConn =
null
;
try
{
httpUrlConn = createHttpURLConnection(requestUrl);
httpUrlConn.setRequestMethod(POST);
addHeaders(httpUrlConn,headers);
// 当有数据需要提交时
if
(
null
!= data) {
outputStream = httpUrlConn.getOutputStream();
// 注意编码格式,防止中文乱码
outputStream.write(data.getBytes(
"UTF-8"
));
outputStream.flush();
}
// 将返回的输入流转换成字符串
InputStream inputStream = httpUrlConn.getInputStream();
InputStreamReader inputStreamReader =
new
InputStreamReader(inputStream, UTF_8);
bufferedReader =
new
BufferedReader(inputStreamReader);
String str =
null
;
while
((str = bufferedReader.readLine()) !=
null
) {
buffer.append(str);
}
return
buffer.toString();
}
catch
(Exception e) {
throw
new
RuntimeException(e);
}
finally
{
close(bufferedReader,outputStream,httpUrlConn);
}
}
public
String httpsGet(String requestUrl) {
StringBuffer buffer =
new
StringBuffer();
BufferedReader bufferedReader =
null
;
OutputStream outputStream =
null
;
HttpsURLConnection httpUrlConn =
null
;
try
{
// 创建SSLContext对象,并使用我们指定的信任管理器初始化
TrustManager[] tm = {
new
NonAuthenticationX509TrustManager() };
SSLContext sslContext = SSLContext.getInstance(
"SSL"
,
"SunJSSE"
);
sslContext.init(
null
, tm,
new
java.security.SecureRandom());
// 从上述SSLContext对象中得到SSLSocketFactory对象
SSLSocketFactory ssf = sslContext.getSocketFactory();
httpUrlConn = (HttpsURLConnection) createHttpURLConnection(requestUrl);
httpUrlConn.setSSLSocketFactory(ssf);
httpUrlConn.setRequestMethod(GET);
// 将返回的输入流转换成字符串
InputStream inputStream = httpUrlConn.getInputStream();
InputStreamReader inputStreamReader =
new
InputStreamReader(inputStream, UTF_8);
bufferedReader =
new
BufferedReader(inputStreamReader);
String str =
null
;
while
((str = bufferedReader.readLine()) !=
null
) {
buffer.append(str);
}
return
buffer.toString();
}
catch
(Exception e) {
throw
new
RuntimeException(e);
}
finally
{
close(bufferedReader,outputStream,httpUrlConn);
}
}
public
String httpsPost(String requestUrl, Map<String,String> headers,String data) {
StringBuffer buffer =
new
StringBuffer();
BufferedReader bufferedReader =
null
;
OutputStream outputStream =
null
;
HttpsURLConnection httpUrlConn =
null
;
try
{
// 创建SSLContext对象,并使用我们指定的信任管理器初始化
TrustManager[] tm = {
new
NonAuthenticationX509TrustManager() };
SSLContext sslContext = SSLContext.getInstance(
"SSL"
,
"SunJSSE"
);
sslContext.init(
null
, tm,
new
java.security.SecureRandom());
// 从上述SSLContext对象中得到SSLSocketFactory对象
SSLSocketFactory ssf = sslContext.getSocketFactory();
httpUrlConn = (HttpsURLConnection) createHttpURLConnection(requestUrl);
httpUrlConn.setSSLSocketFactory(ssf);
httpUrlConn.setRequestMethod(POST);
addHeaders(httpUrlConn,headers);
// 当有数据需要提交时
if
(
null
!= data) {
outputStream = httpUrlConn.getOutputStream();
// 注意编码格式,防止中文乱码
outputStream.write(data.getBytes(UTF_8));
outputStream.flush();
}
// 将返回的输入流转换成字符串
InputStream inputStream = httpUrlConn.getInputStream();
InputStreamReader inputStreamReader =
new
InputStreamReader(inputStream, UTF_8);
bufferedReader =
new
BufferedReader(inputStreamReader);
String str =
null
;
while
((str = bufferedReader.readLine()) !=
null
) {
buffer.append(str);
}
return
buffer.toString();
}
catch
(Exception e) {
throw
new
RuntimeException(e);
}
finally
{
close(bufferedReader,outputStream,httpUrlConn);
}
}
/* ================================================== private methods ================================================== */
private
void
addHeaders(HttpURLConnection httpURLConnection,Map<String,String> headers){
if
(headers==
null
){
return
;
}
for
(Entry<String,String> header:headers.entrySet()){
String key = header.getKey();
String value = header.getValue();
httpURLConnection.addRequestProperty(key, value);
}
}
private
HttpURLConnection createHttpURLConnection(String requestUrl){
URL url =
null
;
HttpURLConnection httpUrlConn =
null
;
try
{
url =
new
URL(requestUrl);
httpUrlConn = (HttpURLConnection) url.openConnection();
}
catch
(Throwable e) {
throw
new
RuntimeException(e);
}
httpUrlConn.setDoOutput(
true
);
httpUrlConn.setDoInput(
true
);
httpUrlConn.setUseCaches(
false
);
return
httpUrlConn;
}
private
void
close(BufferedReader bufferedReader,
OutputStream outputStream,
HttpURLConnection httpUrlConn) {
if
(outputStream!=
null
){
try
{
outputStream.close();
}
catch
(Throwable e){
logger.error(
"关闭流失败!"
,e);
}
}
if
(bufferedReader!=
null
){
try
{
bufferedReader.close();
}
catch
(Throwable e){
logger.error(
"关闭流失败!"
,e);
}
}
if
(httpUrlConn!=
null
){
try
{
httpUrlConn.disconnect();
}
catch
(Throwable e){
logger.error(
"关闭连接失败!"
,e);
}
}
}
}
|
client.NonAuthenticationX509TrustManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package
client;
import
java.security.cert.CertificateException;
import
java.security.cert.X509Certificate;
import
javax.net.ssl.X509TrustManager;
class
NonAuthenticationX509TrustManager
implements
X509TrustManager {
@Override
public
void
checkClientTrusted(X509Certificate[] x509Certificates, String s)
throws
CertificateException {
}
@Override
public
void
checkServerTrusted(X509Certificate[] x509Certificates, String s)
throws
CertificateException {
}
@Override
public
X509Certificate[] getAcceptedIssuers() {
return
new
X509Certificate[
0
];
}
}
|
本文转自 antlove 51CTO博客,原文链接:http://blog.51cto.com/antlove/1680569