httpclient中 RequestEntity和RequestBody区别

httpclient中定制提交的使用

有些情况会要求定制提交内容,例如一些ajax接口,
就要使用 RequestEntity
Java代码
RequestEntity requestEntity=new StringRequestEntity(text);   
  1. post.setRequestEntity(requestEntity);  


这个方法代替了以前直接设置Request body。
RequestEntity是一个接口,有很多实现:
ByteArrayRequestEntity, FileRequestEntity, InputStreamRequestEntity , MultipartRequestEntity, StringRequestEntity

基本上从名字上就可以直接看出功能,可以从字符串,流,文件,字节数组中产生request body。

还有更复杂的Multipart,就是夹杂文件和普通字段的提交。

比如提交一段xml,就可以直接用字符串提交来实现,模拟ajax , http+xml。


RequestEntity和RequestBody区

httpclient的poseMethod提交数据,httpclient3.1采用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;
     }
}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
根据引用\[1\]和引用\[2\]的内容,文件无法上传可能有以下几个原因: 1. 服务器对上传文件大小做了限制,例如Nginx、Apache、IIS等服务器。可以通过修改服务器配置文件来增加文件大小限制。在Nginx,可以在nginx.conf的http{}设置client_max_body_size参数来增加文件大小限制。 2. 网关对上传文件大小做了限制。可以通过添加配置来增加文件大小限制。 3. SpringBoot/Spring对上传文件大小做了限制。可以通过添加配置来增加文件大小限制。 4. 文件确实过大,导致前后端链接超时,自动断开链接。可以在前端做限制,例如限制文件大小或者增加链接超时时间。 综上所述,如果出现"request entity too large"的错误,可能是由于文件大小超过了服务器或者网关的限制,或者文件确实过大导致链接超时。可以根据具体情况来调整服务器、网关或者前端的配置来解决该问题。 #### 引用[.reference_title] - *1* *2* [413 Request Entity Too Large](https://blog.csdn.net/weixin_47162914/article/details/127325350)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [出现 Request Entity Too Large问题的解决方法](https://blog.csdn.net/jewelry008/article/details/77865147)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值