Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot get 'location' header in response using HttpClient

The location header is there, I can see it in browser: enter image description here

I'm using org.apache.httpcomponents.httpclient to send http request with cookie:

```

URI uri = new URIBuilder().setScheme("https").setHost("api.weibo.com").setPath("/oauth2/authorize").setParameter("client_id","3099336849").setParameter("redirect_uri","http://45.78.24.83/authorize").setParameter("response_type", "code").build();
HttpGet req1 = new HttpGet(uri);
RequestConfig config = RequestConfig.custom().setRedirectsEnabled(false).build();
req1.setConfig(config);
req1.setHeader("Connection", "keep-alive");
req1.setHeader("Cookie", cookie);
req1.setHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36");
response = httpclient.execute(req1);

```

I googled a lot and tried enable/disable auto redirect,but it doesn't seem to work for me.So can somebody tell me how to get the location header in response just like the browser did?

like image 771
Araell Avatar asked Oct 30 '22 12:10

Araell


1 Answers

You cannot see 'location' header, because HttpClient followed that redirect immediately - even before giving you that response.

Try disabling redirect while setting up your HttpClient:

HttpClient instance = HttpClientBuilder.create().disableRedirectHandling().build();

Check this URL and you'll see the Location Header:

URI uri = new URIBuilder().setScheme("https").setHost("www.googlemail.com").setPath("/").build();
like image 68
Jan Avatar answered Nov 08 '22 10:11

Jan