Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get cookies from redirect URL

I have one url, which make 2 internal redirects then finally returns ok response.
First URL: : http://www.someurl.com/
Redirect URL 1: : http://www.someurl_1.com/ with response 302
Redirect URL 2: : http://www.someurl_2.com/ with response 302
Final URL: : http://www.finalurl.com/ with response 200
Internally Redirect URL 1 send some cookie to Redirect URL 2. What I have to do is get cookie of which set for Redirect URL 2:.

Here is my java code.

 HttpClient client = HttpClientBuilder.create().build();
      HttpGet get = new HttpGet(myurl);
      get.setHeader("User-Agent", "Mozilla");
      get.setHeader("Accept"," text/html,application/xhtml+xml,application/xml;");
      get.setHeader("Accept-Language", "en-US,en;q=0.8");
      get.setHeader("Accept-Encoding"," gzip, deflate");
      get.setHeader("Connection","keep-alive");
      get.setHeader("Cookie",JSESSIONID+");
//    get.setHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
      Header[] requestheaders   =   get.getAllHeaders();
         System.out.println("requestheaders >>> ");
         for(Header header: requestheaders){
             System.out.println(header.getName()+"-------------------- "+header.getValue());
         }
      HttpResponse response = client.execute(get);
      System.out.println("response 7 "+response);
      System.out.println("Headers are");
      Header[] headers = response.getAllHeaders();
      for (int i = 0; i < headers.length; i++) {
          System.out.println((headers[i].getName()+"___________________"+headers[i].getValue()));
      }

This code gives me final response rather than intermediate redirected response.
So can anyone please suggest me what is better way of doing it.

Other thing I have checked is:-

  1. I have disable redirect, this response gives me very first url of this process.
  2. I have used Jsoup to disable redirect which gives same out put as above.

Whereas fetching such redirected cookie is possible in ruby , i have done that.
But I have to do this in java. httpclient 4.5

like image 933
Kundan Atre Avatar asked Mar 15 '23 04:03

Kundan Atre


1 Answers

This worked for me

  HttpClient client = HttpClientBuilder.create().setRedirectStrategy(new DefaultRedirectStrategy() {   

          public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context)  {
              boolean isRedirect=false;
              try {
                isRedirect = super.isRedirected(request, response, context);
                Header[] requestheaders =   response.getAllHeaders();
                 System.out.println("getAuthToken  >>> ");
                 for(Header header: requestheaders){
                     System.out.println(header.getName()+"-------------------- "+header.getValue());
                    if(header.getName().equalsIgnoreCase("Set-Cookie") && header.getValue().startsWith("auth-token")){
                      System.out.println("Auth_Cookie "+header.getValue().split(";")[0]);
                      auth_token    =   header.getValue().split(";")[0];
                  }
                 }
              } catch (ProtocolException e) {
                e.printStackTrace();
              }
              if (!isRedirect) {
                int responseCode = response.getStatusLine().getStatusCode();
                if (responseCode == 301 || responseCode == 302) {
                  return true;
                }
              }
              return false;
            }
          }).build();
      HttpGet get = new HttpGet(url);
      client.execute(get);

For more technical detail check my blog here.

like image 108
Kundan Atre Avatar answered Mar 24 '23 10:03

Kundan Atre