5

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?

Jan
  • 13,738
  • 3
  • 30
  • 55
Araell
  • 63
  • 1
  • 8

2 Answers2

2

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();
Jan
  • 13,738
  • 3
  • 30
  • 55
  • https://api.weibo.com/oauth2/authorize?client_id=3099336849&redirect_uri=http://45.78.24.83/authorize&response_type=code I got this page.It seems like I didn't authorized when I use the HttpClient to send request. – Araell Dec 22 '15 at 11:15
  • Okay - I tried your code with browser and with code and got response 200 both times as well. – Jan Dec 22 '15 at 11:15
  • So it's not HttpClient not receiving `location` Header, it's the server **not sending it** – Jan Dec 22 '15 at 11:16
  • So I cannot skip auth process with cookie? – Araell Dec 22 '15 at 11:18
  • That's a whole new question... Try setting **all** the headers your browser sent inside your code. – Jan Dec 22 '15 at 11:19
  • I'll try it.Thanks a lot! : ) – Araell Dec 22 '15 at 11:26
0

I found out my real question...I didn't pass the auth process in my code,so I keep getting oauth2 page.After I set all the headers in my request just like the browser did and finally I get the right response.

Araell
  • 63
  • 1
  • 8