用Apache HttpClient实现URL重定向

简介: 很多网站都使用了URL重定向技术,把一个原始请求从一个位置路由到另一个位置。原因可能是多方面的,比如域名转发、URL缩写、隐私保护、在同一网站维持相似的域名等。 本文讲述怎样使用Apache HTTPComponents HttpClient实现URL重定向。
很多网站都使用了URL重定向技术,把一个原始请求从一个位置路由到另一个位置。原因可能是多方面的,比如域名转发、URL缩写、隐私保护、在同一网站维持相似的域名等。
本文讲述怎样使用Apache HTTPComponents HttpClient实现URL重定向。
本文使用的工具:
1. Apache HttpComponents Client 4.3.1
2. JDK 1.7
1、创建Java项目
项目我命名为HttpClientTest,导入如下JAR包:


2、开发
1)创建和配置CloseableHttpClient
CloseableHttpClient是线程安全的,单个实例可用于处理多个HTTP请求。Http Client会自动处理所有的重定向,除非明确地使用disableAutomaticRetries()关闭自动重定向。
2)使用链接创建HttpGet实例,获取重定向。
3)创建本地HTTP执行上下文HttpClientContext。
4)使用Http Client并传递本地实例HttpClientContext,执行HttpGet请求。
5)成功执行请求后,使用上下文对象来获取所有的重定向位置。
6)关闭响应CloseableHttpResponse,释放资源。

点击(此处)折叠或打开

  1. package com.ch.net;
  2.   
  3. import java.io.IOException;
  4. import java.net.URI;
  5. import java.util.List;
  6.   
  7. import org.apache.http.client.ClientProtocolException;
  8. import org.apache.http.client.config.CookieSpecs;
  9. import org.apache.http.client.config.RequestConfig;
  10. import org.apache.http.client.methods.CloseableHttpResponse;
  11. import org.apache.http.client.methods.HttpGet;
  12. import org.apache.http.client.protocol.HttpClientContext;
  13. import org.apache.http.impl.client.CloseableHttpClient;
  14. import org.apache.http.impl.client.HttpClients;
  15.   
  16. public class UrlRedirectionDemo {
  17.     // 浏览器Agent
  18.     public static String USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.151 Safari/535.19";
  19.       
  20.     // 创建并配置HttpClient
  21.     private static final CloseableHttpClient httpClient = HttpClients
  22.             .custom()
  23.             .setUserAgent(USER_AGENT)
  24.             .setDefaultRequestConfig(
  25.                     RequestConfig.custom()
  26.                             .setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY)
  27.                             .build()).build();
  28.       
  29.     /**
  30.      * 根据给定的链接获取所有的重定向位置
  31.      * @param link 给定的链接
  32.      * @return
  33.      * @throws ClientProtocolException
  34.      * @throws IOException
  35.      */
  36.     public ListURI> getAllRedirectLocations(String link) throws ClientProtocolException, IOException{
  37.         ListURI> redirectLocations = null;
  38.         CloseableHttpResponse response = null;
  39.         try{
  40.             HttpClientContext context = HttpClientContext.create();
  41.             HttpGet httpGet = new HttpGet(link);
  42.             response = httpClient.execute(httpGet, context);
  43.               
  44.             // 获取所有的重定向位置
  45.             redirectLocations = context.getRedirectLocations();
  46.         } finally{
  47.             if(response!=null){
  48.                 response.close();
  49.             }
  50.         }
  51.         return redirectLocations;
  52.     }
  53.       
  54.     public static void main(String[] args) throws ClientProtocolException, IOException{
  55.         // 输入URL
  56.         String link = "http://t.cn/zjYwrl3";
  57.         UrlRedirectionDemo demo = new UrlRedirectionDemo();
  58.         ListURI> allRedirectLocations = demo.getAllRedirectLocations(link);
  59.         if(allRedirectLocations!=null){
  60.             System.out.println(link);
  61.             for(URI uri : allRedirectLocations){
  62.                 System.out.println("|\nv\n" + uri.toASCIIString());
  63.             }
  64.         } else{
  65.             System.out.println("Not found!");
  66.         }
  67.     }
  68. }

如果使用默认的User-Agent设置,有些网站会返回HTTP 500状态码错误。一旦网站返回200状态码而且返回的HTML的内容是“500 server error”时,为保证兼容性,应该使用标准的Web浏览器的User-Agent字符串。
500 – 服务器内部错误
200 - 服务器成功返回网页
3、运行
我在新浪微博中找了个URL缩短的地址作为输入,执行后,果然找到了重定向地址。
控制台输出为:

点击(此处)折叠或打开

  1. http://t.cn/zjYwrl3
  2. |
  3. v
  4. http://hero.pongo.cn/
4、验证
用在线URL重定向检测工具测试:


验证OK。
目录
相关文章
|
1月前
|
域名解析 网络协议 开发工具
阿里云DNS常见问题之访问重定向的url访问有问题如何解决
阿里云DNS(Domain Name System)服务是一个高可用和可扩展的云端DNS服务,用于将域名转换为IP地址,从而让用户能够通过域名访问云端资源。以下是一些关于阿里云DNS服务的常见问题合集:
|
1月前
|
网络协议 开发工具 Android开发
应用研发平台EMAS产品常见问题之接入httpdns后 访问重定向的url访问有问题如何解决
应用研发平台EMAS(Enterprise Mobile Application Service)是阿里云提供的一个全栈移动应用开发平台,集成了应用开发、测试、部署、监控和运营服务;本合集旨在总结EMAS产品在应用开发和运维过程中的常见问题及解决方案,助力开发者和企业高效解决技术难题,加速移动应用的上线和稳定运行。
|
10月前
|
应用服务中间件 nginx
Nginx rewrite(URL)地址重定向
Nginx rewrite(URL)地址重定向
372 0
|
15天前
|
JSON 前端开发 API
Apache HttpClient调用Spring3 MVC Restful Web API演示
Apache HttpClient调用Spring3 MVC Restful Web API演示
17 1
|
23天前
|
安全 网络安全 数据安全/隐私保护
Pikachu URL 重定向通关解析
Pikachu URL 重定向通关解析
|
18天前
|
Windows
iis配置http重定向302转发get请求并去掉最后的斜杠/ iis重定向 iis去除url最后的斜杠 iis重定向链接斜杠(已解决)
iis配置http重定向302转发get请求并去掉最后的斜杠/ iis重定向 iis去除url最后的斜杠 iis重定向链接斜杠(已解决)
|
1月前
|
网络协议 应用服务中间件 nginx
nginx 302 301 设置 url 转跳 nginx 资源重定向 nginx tcp 和 http 转发
nginx 代理后端网站,和 网站资源目录重定向到其他连接地址
123 3
|
1月前
|
JSON Java Apache
Spring Cloud Feign 使用Apache的HTTP Client替换Feign原生httpclient
Spring Cloud Feign 使用Apache的HTTP Client替换Feign原生httpclient
|
1月前
|
数据采集 前端开发 Java
利用Scala与Apache HttpClient实现网络音频流的抓取
利用Scala与Apache HttpClient实现网络音频流的抓取
|
1月前
|
Java Apache
Apache HttpClient 4.5设置超时时间
Apache HttpClient 4.5设置超时时间

推荐镜像

更多