如何编码MultipartEntityBuilder而不是使用单独的方法?
我尝试了多种方法,但不断获得403状态代码。 这是一个例子:
private void doPost(int type) throws UnsupportedEncodingException {
HttpPost post = new HttpPost("http://amazon.com");
post.setHeader(HTTP.CONTENT_TYPE, "application/x-www-form-urlencoded;charset=UTF-8");
if(type == 1)
post.setEntity(useBuilder());
else if(type == 2)
post.setEntity(useEncoder()); //<---- wanting to not have to do this
}
private HttpEntity useBuilder(){
MultipartEntityBuilder me = MultipartEntityBuilder.create().setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
me.addTextBody("name1", "value1");
me.addTextBody("name2", "value2");
return me.build();
}
private UrlEncodedFormEntity useEncoder() throws UnsupportedEncodingException {
List<NameValuePair> list = new ArrayList<NameValuePair>();
list.add(new BasicNameValuePair("name1", "value1"));
list.add(new BasicNameValuePair("name2", "value2"));
return new UrlEncodedFormEntity(list, "UTF-8");
}