1

I'm sending an http get/head request using Apache HttpClient 4.x. I'm sending a request with a url like "http://example.com/getAccessToken". I'm expecting the response to be a redirect url with parameters in the returned url like "http://redirecturl.com/?code=accessTokenStuff". I want to be able to parse the response redirect url parameters, i.e. I want to get "accessTokenStuff". How can I do that?

HttpClient client = new DefaultHttpClient();
            HttpHead request = new HttpHead(authUrl);
            HttpResponse response = client.execute(request);
            System.out.println(response.getStatusLine().getStatusCode());//returns 200

            request.releaseConnection();

In a nutshell: what I want is executing an original url and then getting the result which is another url that has a parameter called "code". Then I want to get the value of that parameter.

EDIT:

I also tried this but it returns the same original URL

DefaultHttpClient client = new DefaultHttpClient();
            HttpParams params = client.getParams();
            HttpClientParams.setRedirecting(params, false);
            HttpGet request = new HttpGet(authUrl);
            HttpResponse response = client.execute(request);
            String location = response.getLastHeader("Location").getValue();//returns same original url
            System.out.println(location);
            request.releaseConnection();

Setting HttpClientParams.setRedirecting(params, true); return null

Jack Twain
  • 6,273
  • 15
  • 67
  • 107

1 Answers1

0

Http response do not take a form of a "redirect url". Redirect and response are both different (but related) concepts. Redirect usually means "get your response from this address instead of original one".

Having said this, you can prevent HttpClient from following a redirect, see this answer: How to prevent apache http client from following a redirect

When your HttpClient is not following the redirect, you can inspect the 'Location:' header of its response, eg:

HeaderIterator iterator = httpResponse.headerIterator("Location");
while(iterator.hasNext()) {
  Header header = iterator.nextHeader();
  String redirectUrl = header.getValue();
}
Community
  • 1
  • 1
gerrytan
  • 40,313
  • 9
  • 84
  • 99
  • I don't really understand this. What I want is executing an original url and then getting the result which is another url that has a parameter called "code". Then I want to get the value of that parameter. – Jack Twain Aug 06 '13 at 22:59
  • getting the value of the "location" header returns nothing – Jack Twain Aug 06 '13 at 23:12
  • I set the client not to follow the redirect url, but when I access the Location header I get the old URL not the redirect url. – Jack Twain Aug 06 '13 at 23:25
  • The fact you mentioned is against known behavior of http redirect. When you inspect the **response** header with key "Location:", you will get the "redirect URL". See: http://en.wikipedia.org/wiki/URL_redirection#HTTP_status_codes_3xx – gerrytan Aug 07 '13 at 01:25