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?

1
  • Please share the code you used for trying to disable redirect. Commented Dec 22, 2015 at 10:45

2 Answers 2

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();
Sign up to request clarification or add additional context in comments.

6 Comments

api.weibo.com/oauth2/… I got this page.It seems like I didn't authorized when I use the HttpClient to send request.
Okay - I tried your code with browser and with code and got response 200 both times as well.
So it's not HttpClient not receiving location Header, it's the server not sending it
So I cannot skip auth process with cookie?
That's a whole new question... Try setting all the headers your browser sent inside your code.
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.