HttpClients 如何自动处理重定向
2016-01-06 10:22
519 查看
首先说说HttpClient和浏览器的区别
我们从浏览器发起一笔请求,浏览器则会帮你处理重定向、缓存等事情。这也就是为什么用浏览器表单post提交后,不管服务端如何重定向,都能正常接收到服务端返回的数据。
但是用HttpClient呢,你会发现,请求后,会返回302,因为POST方式提交HttpClient是不会帮你处理重定向的。这时候怎么办呢?
方法一:(自己手动处理)
HttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost= new HttpPost(http://ip:port/xxx);
CloseableHttpResponse response = httpclient.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
System.out.println("statusCode=="+statusCode); //返回码
Header header=response.getFirstHeader("Location");
//重定向地址
String location = header.getValue();
System.out.println(location);
//然后再对新的location发起请求即可
HttpGet httpGet = new HttpGet(location);
CloseableHttpResponse response2 = httpclient.execute(httpGet);
System.out.println("返回报文"+EntityUtils.toString(response2.getEntity(), "UT-F-8"));
方法二:(已有工具类)
HttpClientBuilder builder = HttpClients.custom()
.disableAutomaticRetries() //关闭自动处理重定向
.setRedirectStrategy(new LaxRedirectStrategy());//利用LaxRedirectStrategy处理POST重定向问题
CloseableHttpClient client = builder.build();
HttpPost httpPost= new HttpPost(http://ip:port/xxx);
CloseableHttpResponse response = client.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
System.out.println("statusCode=="+statusCode); //返回码
System.out.println("返回报文"+EntityUtils.toString(response.getEntity(), "UT-F-8"));
首先说说HttpClient和浏览器的区别
我们从浏览器发起一笔请求,浏览器则会帮你处理重定向、缓存等事情。这也就是为什么用浏览器表单post提交后,不管服务端如何重定向,都能正常接收到服务端返回的数据。
但是用HttpClient呢,你会发现,请求后,会返回302,因为POST方式提交HttpClient是不会帮你处理重定向的。这时候怎么办呢?
方法一:(自己手动处理)
HttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost= new HttpPost(http://ip:port/xxx);
CloseableHttpResponse response = httpclient.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
System.out.println("statusCode=="+statusCode); //返回码
Header header=response.getFirstHeader("Location");
//重定向地址
String location = header.getValue();
System.out.println(location);
//然后再对新的location发起请求即可
HttpGet httpGet = new HttpGet(location);
CloseableHttpResponse response2 = httpclient.execute(httpGet);
System.out.println("返回报文"+EntityUtils.toString(response2.getEntity(), "UT-F-8"));
方法二:(已有工具类)
HttpClientBuilder builder = HttpClients.custom()
.disableAutomaticRetries() //关闭自动处理重定向
.setRedirectStrategy(new LaxRedirectStrategy());//利用LaxRedirectStrategy处理POST重定向问题
CloseableHttpClient client = builder.build();
HttpPost httpPost= new HttpPost(http://ip:port/xxx);
CloseableHttpResponse response = client.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
System.out.println("statusCode=="+statusCode); //返回码
System.out.println("返回报文"+EntityUtils.toString(response.getEntity(), "UT-F-8"));
相关文章推荐
- python中http请求方法库汇总
- C#网络编程之同步异步
- 元旦闲得:网络流量的经济属性浅解
- Namespace:Openstack的网络实现
- Python 写网络爬虫思路分析
- httpclient 调用WebAPI
- mac 网络共享 wifi共享
- python网络编程之SimpleServer/Client
- Linux下的wget和curl如何使用http proxy
- 山东大学计算机网络复习纲要
- 网络连接
- JpGraph使用详解http://5ydycm.blog.51cto.com/115934/177498 http://www.cnblogs.com/txw1958/archive/2013/08/18/php-charts.html
- HTTP中Get与Post的区别
- COMODO网络安全专业版8下载
- XMLHttpRequest
- http和https的区别
- COMODO网络安全套装下载
- NTP时间服务器|机架式时间服务器|网络对时服务器
- COMODO网络安全完全版8下载
- [连载]《C#通讯(串口和网络)框架的设计与实现》- 13.中英文版本切换设计