HttpComponents 重定向问题分析和解决
一、 起因:
最近在学习使用HttpComponents登陆网站,这样的登陆毫无疑问使用的是post方法提交。
但是在登录的过程中,有的会返回302状态代码。这是因为该网站使用了重定向链接造成的。
于是开始了漫长的查阅资料的过程。
二、 过程:
国内关于HttpComponents处理302状态问题的例子不多,基本上都是一些简单的例子或者API的翻译。
后来去google搜索,才解决了问题。
三、 解决方法:
1. 原因所在:
httpcomponents 在处理重定向问题时,默认的情况下是有限制的:
对于 HEAD 和 GET 方法, HttpComponents 会自动做重定向处理;
对于 POST 和 PUT 方法, HttpComponents 不会进行自动重定向处理,这需要用户自己设定才行。
下面是 DefaultRedirectStrategy 中的描述:
Default implementation of RedirectStrategy
. This strategy honors the restrictions on automatic redirection of entity enclosing methods such as POST and PUT imposed by the HTTP specification. 302 Moved Temporarily, 301 Moved Permanently and 307 Temporary Redirect status codes will result in an automatic redirect of HEAD and GET methods only. POST and PUT methods will not be automatically redirected as requiring user confirmation.
The restriction on automatic redirection of POST methods can be relaxed by using LaxRedirectStrategy
instead of DefaultRedirectStrategy
.
在这段描述中可以知道, 想要使 POST 和 PUT 方法可以自动处理重定向链接,使用 LaxRedirectStrategy 类即可。
2. 代码的用法:
DefaultHttpClient client = new DefaultHttpClient(); LaxRedirectStrategy redirectStrategy = new LaxRedirectStrategy(); // 设定自己需要的重定向策略 client.setRedirectStrategy(redirectStrategy); // 创建登陆form List<NameValuePair> formparams = new ArrayList<NameValuePair>(); formparams.add(new BasicNameValuePair("username", USERNAME)); formparams.add(new BasicNameValuePair("password", PASSWORD)); UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(formparams, "UTF-8"); // 创建登陆请求 HttpPost loginPost = new HttpPost(URL_LOGIN); loginPost.setEntity(formEntity); // 执行请求, // 在使用默认的重定向策略是,状态代码返回的是 302 // 使用了重定向策略后, 状态代码返回的是 200 HttpResponse loginResponse = client.execute(loginPost); System.out.println("登录请求放回状态代码: " + loginResponse.getStatusLine().getStatusCode());
相关推荐
httpcomponents-jar包;httpcomponents-jar包;httpcomponents-jar包;httpcomponents-jar包;httpcomponents-jar包;httpcomponents-jar包
Httpcomponents 是Apache Jakarta 項目的一個子項目 HttpComponents项目是HTTP协议的几个方面的底层库。在服务器或客户端通讯有高级需求的用户可能发现,对于建构定制的HTTP协议服务,这是一个有用的工具集
HttpComponents.zip HttpComponents.zip
HttpComponents用法解析 Java模拟浏览器发送请求
httpcomponents-core-4.2.1 httpcomponents-client-4.2 httpcore4.2 org.apache.http
apache-httpcomponents-httpmime.jar
里面包含httpcomponents所有jar包,足够开发使用的,很全面的,很好用!
httpcomponents apache开源http组件,主要用于相关http客户端通讯
HttpComponents也就是以前的httpclient项目,可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端/服务器编程工具包,并且它支持 HTTP 协议最新的版本和建议。 以下列出的是 HttpClient 提供的主要的...
httpclient httpget jar 包 下载
Apache HttpComponents提供HTTP接口,用来向目标网址提交HTTP请求,以获取网页的内容
java-httpcomponents
Apache HttpComponents 已经取代了 HttpClient 。 官方内容:The Commons HttpClient project is now end of life, and is no longer being developed. It has been replaced by the Apache HttpComponents project ...
httpcomponents-client-4.2.5,httpcomponents-client-4.2.5,包括文档,代码示例亲测可用。
httpcomponents client apache开源源码基于httpcomponents-core,主要用于http客户端通讯
httpcomponents系列jar包,httpclient、httpcore、httpmine
httpcomponents-client-4.5.12.zip包含了多个jar包。 欢迎大家下载,大家也可关注我的博客,欢迎一起交流,如有疑问请留言!
HttpComponents-4.3.2帮助文档 chm格式, 超好用
网络爬虫需要的org.apache.http包)(httpcomponents-client-4.2.5-bin.tar)