HttpClients 如何自动处理重定向
2017-05-24 00:54
429 查看
HttpClient只是模拟一次tcp请求,浏览器则会帮你处理重定向、缓存等事情。这也就是为什么用浏览器表单post提交后,不管服务端如何重定向,都能正常接收到服务端返回的数据。
但是用HttpClient呢,你会发现,请求后,会返回302,因为POST方式提交HttpClient是不会帮你处理重定向的。
方法一:(自己手动处理)
HttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost= new HttpPost(http://ip:sport/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:sport/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"));
4000
但是用HttpClient呢,你会发现,请求后,会返回302,因为POST方式提交HttpClient是不会帮你处理重定向的。
方法一:(自己手动处理)
HttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost= new HttpPost(http://ip:sport/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:sport/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"));
4000
相关文章推荐
- HttpClients 如何自动处理重定向
- java中httpclent以post方式提交请求时,http返回码是302重定向时,不自动跳转的处理
- 如何实现301的跳转?当输入域名http://xxx.com的时候自动重定向到www上去
- 处理HTTP响应(响应码、响应头、请求重定向、自动刷新/跳转)
- java网络下载中如何处理http重定向
- java中httpclent以post方式提交请求时,http返回码是302重定向时,不自动跳转的处理
- 如何在Spring Boot中,使用Https提供服务,并将Http请求自动重定向到Https。
- 在LoadRunner脚本中处理HTTP重定向
- 当用Servlet来处理http请求页面时,如何使HTML页面中的
- 如何关闭WebEditor在线编辑器的远程自动上传功能http://www.jn10086.cn/boke/article.asp?id=231
- httpclient 4 POST 自动重定向
- commons-httpclient.jar的使用(处理页面重定向)
- 如何将Exchange OWA http重定向到https
- 如何设置IIS以便HttpHandler处理相关文件不存在的情形
- 用Excel打开csv文件时,如何处理数字内容展现会自动转换格式的问题
- 用Excel打开csv文件时,如何处理数字内容展现会自动转换格式的问题
- Nokia S60如何处理302 HTTP状态[J2ME]
- 如何处理bgswitch,壁纸自动换
- Nokia S60如何处理302 HTTP状态[J2ME]
- Nokia S60如何处理302 HTTP状态[J2ME]