在处理自动提交表单的业务中往往要用到httpclient的poseMethod提交数据,httpclient3.1采用RequestEntity接口替代以前的RequestBody。
RequestEntity :
RequestBody:
在某些网站用RequestEntity方式请求有会失败,改成RequestBody就可以,可能就是这个原因。
RequestEntity :
HttpClient client = new HttpClient();
PostMethod post = new PostMethod(redirectUrl);
Part[] parts = {
new StringPart("user", “aaa”),
new StringPart("password", "bbb")
};
post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));
client.executeMethod(post);
String result = post.getResponseBodyAsString();
RequestBody:
HttpClient client = new HttpClient();
PostMethod post = new PostMethod(redirectUrl);
NameValuePair[] pairs = {
new NameValuePair("user", “aaa”),
new NameValuePair("password", “bbb”)
};
post.setRequestBody(pairs);
client.executeMethod(post);
String result = post.getResponseBodyAsString();
在某些网站用RequestEntity方式请求有会失败,改成RequestBody就可以,可能就是这个原因。