Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.80 KB | None | 0 0
  1. package sparrow;
  2.  
  3. import org.apache.http.HttpHost;
  4. import org.apache.http.HttpResponse;
  5. import org.apache.http.client.methods.HttpGet;
  6. import org.apache.http.client.methods.HttpUriRequest;
  7. import org.apache.http.client.protocol.ClientContext;
  8. import org.apache.http.impl.client.DefaultHttpClient;
  9. import org.apache.http.protocol.BasicHttpContext;
  10. import org.apache.http.protocol.ExecutionContext;
  11. import org.apache.http.protocol.HttpContext;
  12. import org.apache.log4j.Logger;
  13.  
  14. import java.io.IOException;
  15.  
  16.  
  17. /**
  18.  * This class will attempt to follow a link completely until we get to the final URL
  19.  * after all the 302 redirects. A bitly link may refer to a link that actually 302's to something
  20.  * else so we need to follow the trail all the way.
  21.  * @author Jim Plush
  22.  *
  23.  */
  24. public class FinalUrlGrabber {
  25.  
  26.   static Logger logger = Logger.getLogger(FinalUrlGrabber.class);
  27.  
  28.   public static String getURL(DefaultHttpClient httpClient, String startURL)
  29.   {
  30.    
  31.    
  32.    
  33.     int cnt = 0;
  34.     String finalURL = null;
  35.     while(!startURL.equals(finalURL))
  36.     {
  37.      
  38.       if(cnt > 1) {
  39.         SparrowRunner.multiple302.incrementAndGet();
  40.       }
  41.      
  42.       try {
  43.         finalURL = followURL(httpClient, startURL);
  44.         startURL = followURL(httpClient, finalURL);
  45.       } catch(IOException e) {
  46.         logger.info(e.toString());
  47.         return null;
  48.       }
  49.       if(cnt > 20) {
  50.         return null;
  51.       }
  52.       cnt++;
  53.       //logger.info("START: "+startURL + " FINAL: "+finalURL);
  54.  
  55.     }
  56.    
  57.     return finalURL;
  58.      
  59.  
  60.  
  61.   }
  62.  
  63.  
  64.   private static String followURL(DefaultHttpClient httpClient, String startURL) throws IOException
  65.   {
  66.     String finalURL = null;
  67.     HttpGet httpget = null;
  68.     HttpHost target = null;
  69.     HttpUriRequest req = null;
  70.     try {
  71.  
  72.       HttpContext localContext = new BasicHttpContext();
  73.       localContext.setAttribute(ClientContext.COOKIE_STORE,HttpClientFetcher.emptyCookieStore);
  74.       httpget = new HttpGet(startURL);
  75.  
  76.      
  77.       httpget.setHeader("Range", "bytes=0-5000");
  78.       HttpResponse response = httpClient.execute(httpget, localContext);
  79.  
  80.       HttpHost host =  (HttpHost)localContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
  81.       HttpUriRequest finalRequest =   (HttpUriRequest)localContext.getAttribute(ExecutionContext.HTTP_REQUEST);
  82.  
  83.       target = (HttpHost) localContext.getAttribute( ExecutionContext.HTTP_TARGET_HOST );
  84.       req = (HttpUriRequest) localContext.getAttribute( ExecutionContext.HTTP_REQUEST );
  85.      
  86.       finalURL = target+""+req.getURI();
  87.      
  88.       return finalURL;
  89.  
  90.     } catch(Exception e) {
  91.  
  92.       throw new IOException("UNABLE TO GET LINK: "+startURL+ " "+e.toString());
  93.  
  94.     } finally {
  95.       if(httpget != null) { try { httpget.abort(); } catch(Exception e) {}}
  96.     }
  97.  
  98.   }
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement