httpclient中定制提交的使用
有些情况会要求定制提交内容,例如一些ajax接口,
就要使用 RequestEntity
Java代码
RequestEntity requestEntity=new StringRequestEntity(text);
- post.setRequestEntity(requestEntity);
这个方法代替了以前直接设置Request body。
RequestEntity是一个接口,有很多实现:
ByteArrayRequestEntity, FileRequestEntity, InputStreamRequestEntity
, MultipartRequestEntity, StringRequestEntity
基本上从名字上就可以直接看出功能,可以从字符串,流,文件,字节数组中产生request body。
还有更复杂的Multipart,就是夹杂文件和普通字段的提交。
比如提交一段xml,就可以直接用字符串提交来实现,模拟ajax , http+xml。
httpclient中定制提交的使用
有些情况会要求定制提交内容,例如一些ajax接口,
就要使用 RequestEntity
Java代码
RequestEntity requestEntity=new StringRequestEntity(text);
- post.setRequestEntity(requestEntity);
这个方法代替了以前直接设置Request body。
RequestEntity是一个接口,有很多实现:
ByteArrayRequestEntity, FileRequestEntity, InputStreamRequestEntity
基本上从名字上就可以直接看出功能,可以从字符串,流,文件,字节数组中产生request body。
还有更复杂的Multipart,就是夹杂文件和普通字段的提交。
比如提交一段xml,就可以直接用字符串提交来实现,模拟ajax , http+xml。
RequestEntity和RequestBody区别
代码片段(2)[全屏查看所有代码]
1. HttpRequestEntityUtils.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
|
package
com.allcam.sys.thirdplat.kmc;
import
java.io.IOException;
import
java.io.InputStream;
import
org.apache.commons.httpclient.HttpClient;
import
org.apache.commons.httpclient.HttpStatus;
import
org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
import
org.apache.commons.httpclient.methods.GetMethod;
import
org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import
org.apache.commons.httpclient.methods.PostMethod;
import
org.apache.commons.httpclient.methods.RequestEntity;
import
org.slf4j.Logger;
import
org.slf4j.LoggerFactory;
public
class
HttpRequestEntityUtils
{
protected
static
final
int
httpConnectionTimeOut =
30
;
protected
final
static
Logger log = LoggerFactory.getLogger(HttpRequestEntityUtils.
class
);
public
static
String post(String url, String params)
throws
IOException
{
String body =
null
;
HttpClient httpClient =
null
;
PostMethod method =
null
;
try
{
httpClient =
new
HttpClient();
method =
new
PostMethod(url);
// 链接超时30秒
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(
1000
* httpConnectionTimeOut);
// 读取超时30秒
httpClient.getHttpConnectionManager().getParams().setSoTimeout(
1000
* httpConnectionTimeOut);
//
log.info(
"create http post:"
+ url);
RequestEntity requestEntity =
new
ByteArrayRequestEntity(params.getBytes(
"UTF-8"
));
method.setRequestEntity(requestEntity);
if
(method.getStatusCode() == HttpStatus.SC_OK)
{
body = method.getResponseBodyAsString();
}
else
{
log.error(
"method.getStatusCode()"
+ method.getStatusCode());
}
}
catch
(IOException e)
{
log.error(
"IOException"
, e);
throw
e;
}
catch
(Exception e)
{
log.error(
"Exception"
, e);
throw
e;
}
finally
{
if
(method !=
null
)
{
method.releaseConnection();
}
}
return
body;
}
public
static
String postPushStream(String url, InputStream inputStream)
throws
IOException
{
String body =
null
;
HttpClient httpClient =
null
;
PostMethod method =
null
;
try
{
httpClient =
new
HttpClient();
method =
new
PostMethod(url);
// 链接超时30秒
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(
1000
* httpConnectionTimeOut);
// 读取超时30秒
httpClient.getHttpConnectionManager().getParams().setSoTimeout(
1000
* httpConnectionTimeOut);
//
log.info(
"create http post:"
+ url);
RequestEntity requestEntity =
new
InputStreamRequestEntity(inputStream,
"UTF-8"
);
method.setRequestEntity(requestEntity);
if
(method.getStatusCode() == HttpStatus.SC_OK)
{
body = method.getResponseBodyAsString();
}
else
{
log.error(
"method.getStatusCode()"
+ method.getStatusCode());
}
}
catch
(IOException e)
{
log.error(
"IOException"
, e);
throw
e;
}
catch
(Exception e)
{
log.error(
"Exception"
, e);
throw
e;
}
finally
{
if
(method !=
null
)
{
method.releaseConnection();
}
}
return
body;
}
public
static
InputStream postGetStream(String url, String params)
throws
IOException
{
InputStream body =
null
;
HttpClient httpClient =
null
;
PostMethod method =
null
;
try
{
httpClient =
new
HttpClient();
method =
new
PostMethod(url);
// 链接超时30秒
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(
1000
* httpConnectionTimeOut);
// 读取超时30秒
httpClient.getHttpConnectionManager().getParams().setSoTimeout(
1000
* httpConnectionTimeOut);
//
log.info(
"create http post:"
+ url);
RequestEntity requestEntity =
new
ByteArrayRequestEntity(params.getBytes(
"UTF-8"
));
method.setRequestEntity(requestEntity);
if
(method.getStatusCode() == HttpStatus.SC_OK)
{
body = method.getResponseBodyAsStream();
}
else
{
log.error(
"method.getStatusCode()"
+ method.getStatusCode());
}
}
catch
(IOException e)
{
log.error(
"IOException"
, e);
throw
e;
}
catch
(Exception e)
{
log.error(
"Exception"
, e);
throw
e;
}
finally
{
if
(method !=
null
)
{
method.releaseConnection();
}
}
return
body;
}
public
static
String get(String url)
throws
IOException
{
String body =
null
;
HttpClient httpClient =
null
;
GetMethod method =
null
;
try
{
httpClient =
new
HttpClient();
method =
new
GetMethod(url);
// 链接超时30秒
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(
1000
* httpConnectionTimeOut);
// 读取超时30秒
httpClient.getHttpConnectionManager().getParams().setSoTimeout(
1000
* httpConnectionTimeOut);
//
log.info(
"create http get:"
+ url);
if
(method.getStatusCode() == HttpStatus.SC_OK)
{
body = method.getResponseBodyAsString();
}
else
{
log.error(
"method.getStatusCode()"
+ method.getStatusCode());
}
}
catch
(IOException e)
{
log.error(
"IOException"
, e);
throw
e;
}
catch
(Exception e)
{
log.error(
"Exception"
, e);
throw
e;
}
finally
{
if
(method !=
null
)
{
method.releaseConnection();
}
}
return
body;
}
}
|
2. HttpRequestBodyUtils.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
|
package
com.allcam.sys.thirdplat.kmc;
import
java.io.IOException;
import
java.io.UnsupportedEncodingException;
import
java.util.ArrayList;
import
java.util.List;
import
java.util.Map;
import
java.util.Set;
import
org.apache.http.HttpEntity;
import
org.apache.http.HttpResponse;
import
org.apache.http.NameValuePair;
import
org.apache.http.ParseException;
import
org.apache.http.client.ClientProtocolException;
import
org.apache.http.client.entity.UrlEncodedFormEntity;
import
org.apache.http.client.methods.HttpGet;
import
org.apache.http.client.methods.HttpPost;
import
org.apache.http.client.methods.HttpUriRequest;
import
org.apache.http.impl.client.DefaultHttpClient;
import
org.apache.http.message.BasicNameValuePair;
import
org.apache.http.protocol.HTTP;
import
org.apache.http.util.EntityUtils;
import
org.slf4j.Logger;
import
org.slf4j.LoggerFactory;
public
class
HttpRequestBodyUtils
{
protected
final
static
Logger log = LoggerFactory.getLogger(HttpRequestBodyUtils.
class
);
public
static
String post(String url, Map<String, String> params)
throws
IOException
{
DefaultHttpClient httpclient =
new
DefaultHttpClient();
String body =
null
;
log.info(
"create http post:"
+ url);
HttpPost post = postForm(url, params);
body = invoke(httpclient, post);
httpclient.getConnectionManager().shutdown();
return
body;
}
public
static
String get(String url)
throws
IOException
{
DefaultHttpClient httpclient =
new
DefaultHttpClient();
String body =
null
;
log.info(
"create http post:"
+ url);
HttpGet get =
new
HttpGet(url);
body = invoke(httpclient, get);
httpclient.getConnectionManager().shutdown();
return
body;
}
private
static
String invoke(DefaultHttpClient httpclient, HttpUriRequest httppost)
throws
IOException
{
HttpResponse response = sendRequest(httpclient, httppost);
String body = paseResponse(response);
return
body;
}
private
static
String paseResponse(HttpResponse response)
throws
IOException
{
log.info(
"get response from http server.."
);
HttpEntity entity = response.getEntity();
log.info(
"response status: "
+ response.getStatusLine());
String charset = EntityUtils.getContentCharSet(entity);
log.info(charset);
String body =
null
;
try
{
body = EntityUtils.toString(entity);
log.info(body);
}
catch
(ParseException e)
{
log.error(
"ParseException"
, e);
throw
e;
}
catch
(IOException e)
{
log.error(
"IOException"
, e);
throw
e;
}
catch
(Exception e)
{
log.error(
"Exception"
, e);
throw
e;
}
return
body;
}
private
static
HttpResponse sendRequest(DefaultHttpClient httpclient, HttpUriRequest httpost)
throws
IOException
{
log.info(
"execute post..."
);
HttpResponse response =
null
;
try
{
response = httpclient.execute(httpost);
}
catch
(ClientProtocolException e)
{
log.error(
"ClientProtocolException"
, e);
throw
e;
}
catch
(IOException e)
{
log.error(
"IOException"
, e);
throw
e;
}
catch
(Exception e)
{
log.error(
"Exception"
, e);
throw
e;
}
return
response;
}
private
static
HttpPost postForm(String url, Map<String, String> params)
throws
UnsupportedEncodingException
{
HttpPost httpost =
new
HttpPost(url);
List<NameValuePair> nvps =
new
ArrayList<NameValuePair>();
Set<String> keySet = params.keySet();
for
(String key : keySet)
{
nvps.add(
new
BasicNameValuePair(key, params.get(key)));
}
try
{
log.info(
"set utf-8 form entity to httppost"
);
httpost.setEntity(
new
UrlEncodedFormEntity(nvps, HTTP.UTF_8));
}
catch
(UnsupportedEncodingException e)
{
log.error(
"UnsupportedEncodingException"
, e);
throw
e;
}
catch
(Exception e)
{
log.error(
"Exception"
, e);
throw
e;
}
return
httpost;
}
}
|