读书人

关于httpclient定制对象RequestEntity

发布时间: 2012-12-19 14:13:14 作者: rapoo

关于httpclient定制对象RequestEntity和RequestBody引发的问题
在处理自动提交表单的业务中往往要用到httpclient的poseMethod提交数据,httpclient3.1采用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就可以,可能就是这个原因。

读书人网 >编程

热点推荐