Many websites use URL redirection technology to route an original request from one location to another. The reasons may be multiple, such as domain name forwarding, URL abbreviations, privacy protection, maintaining similar domain names on the same site, and so on.
This article describes how to implement URL redirection using Apache httpcomponents httpclient.
The tools used in this article:
1. Apache httpcomponents Client 4.3.1
2. JDK 1.7
1. Create a Java project
Project I named Httpclienttest, import the following jar package:
2. Development
1) Create and configure Closeablehttpclient
Closeablehttpclient is thread-safe, and a single instance can be used to handle multiple HTTP requests. The Http client automatically handles all redirects unless you explicitly use Disableautomaticretries () to turn off automatic redirection.
2) Use the link to create the HttpGet instance to get the redirect.
3) Create local HTTP execution context Httpclientcontext.
4) Use the HTTP client and pass the local instance Httpclientcontext to execute the httpget request.
5) After successfully executing the request, use the context object to get all the redirect locations.
6) Close the response closeablehttpresponse and release the resource.
Packagecom.ch.net; Importjava.io.IOException;ImportJava.net.URI;Importjava.util.List; Importorg.apache.http.client.ClientProtocolException;ImportOrg.apache.http.client.config.CookieSpecs;ImportOrg.apache.http.client.config.RequestConfig;ImportOrg.apache.http.client.methods.CloseableHttpResponse;ImportOrg.apache.http.client.methods.HttpGet;ImportOrg.apache.http.client.protocol.HttpClientContext;Importorg.apache.http.impl.client.CloseableHttpClient;Importorg.apache.http.impl.client.HttpClients; Public classUrlredirectiondemo {//Browser Agent Public StaticString 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 "; //Create and configure HttpClient Private Static FinalCloseablehttpclient httpClient =httpclients. Custom (). Setuseragent (User_agent). Setdefaultrequestconfig ( Requestconfig.custom (). Setcookiespec (cookiespecs.browser_compatibility) . Build ()). build (); /*** Get all redirect locations based on a given link *@paramlink to given links *@return * @throwsclientprotocolexception *@throwsIOException*/ Publiclist<uri> getallredirectlocations (String link)throwsclientprotocolexception, ioexception{List<URI> redirectlocations =NULL; Closeablehttpresponse Response=NULL; Try{Httpclientcontext Context=httpclientcontext.create (); HttpGet HttpGet=Newhttpget (link); Response=Httpclient.execute (httpget, context); //get all REDIRECT LocationsRedirectlocations =context.getredirectlocations (); } finally{ if(response!=NULL) {response.close (); } } returnredirectlocations; } Public Static voidMain (string[] args)throwsclientprotocolexception, ioexception{//Enter URLString link = "Http://t.cn/zjYwrl3"; Urlredirectiondemo Demo=NewUrlredirectiondemo (); List<URI> allredirectlocations =demo.getallredirectlocations (link); if(allredirectlocations!=NULL) {System.out.println (link); for(URI uri:allredirectlocations) {System.out.println ("|\nv\n" +uri.toasciistring ()); } } Else{System.out.println ("Not found!"); } } }
If you use the default User-agent setting, some websites will return an HTTP 500 status code error. Once the site returns a 200 status code and the content of the returned HTML is "$ server error", the standard Web browser's user-agent string should be used to ensure compatibility.
500– Server Internal Error
200-Server successfully returned to Web page
3. Operation
I found a URL shortened address as input in Sina Weibo, and after execution, I did find the redirect address.
The console output is:
http://| v http: // hero.pongo.cn/
4. Verification
Test with the online URL redirection detection Tool:
Verify OK.
Httpclient_ using Apache HttpClient for URL redirection