Kundan Atre
Kundan Atre

Reputation: 3961

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

Upvotes: 4

Views: 5968

Answers (3)

crowmagnumb
crowmagnumb

Reputation: 7117

I had the same issue where I had a number of redirects but I needed the cookies from each response to be passed on to the redirects. I could have probably done more work to figure out what cookies each redirect really needed but I just built up a map of all the cookies and each redirect got the full set of cookies from the combined redirects before it. Here is my code...

import java.util.HashMap;
import java.util.Map;

import org.apache.http.Header;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.ProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.RedirectStrategy;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultRedirectStrategy;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.protocol.HttpContext;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

...

    RestTemplate restTemplate = new RestTemplate();
    HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
    RedirectStrategy rs = new DefaultRedirectStrategy() {
        Map<String, Header> cookies = new HashMap<>();

        @Override
        public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response,
                                          HttpContext context) throws ProtocolException {
            //
            // Get the cookies out of the response so we can inject them into the redirect.
            //
            for (Header header : response.getHeaders("Set-Cookie")) {
                this.cookies.put(header.getName(), header);
            }

            HttpUriRequest redirect = super.getRedirect(request, response, context);

            for (Header cookie : this.cookies.values()) {
                redirect.addHeader("Cookie", cookie.getValue());
            }
            return redirect;
        }
    };

    final HttpClient httpClient = HttpClientBuilder.create().setRedirectStrategy(rs).build();
    factory.setHttpClient(httpClient);
    restTemplate.setRequestFactory(factory);

    ResponseEntity<String> response = restTemplate.getForEntity("<my_url>", String.class);

Upvotes: 1

Kundan Atre
Kundan Atre

Reputation: 3961

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.

Upvotes: 4

kmandov
kmandov

Reputation: 3209

If you want more control over the redirection behavior, you can probably change the RedirectStrategy of the http client.

You can create your own RedirectStrategy and use it:

HttpClient instance = HttpClientBuilder.create()
                     .setRedirectStrategy(new LaxRedirectStrategy()).build();

Upvotes: 3

Related Questions