3

I'm trying to call a web server that is using relative URL redirects for some of the calls. This of course isn't working with DefaultHttpClient as it isn't treating it as a relative URL. I've gotten as far as implementing a RedirectHandler in an attempt to catch the redirect and add in the base call but I can't work out how to get the location of the redirect.

With the following method how do I go about finding out where I am being redirected to? I can't find any fields on either response or context that have what I need and I don't know where else to look.

public URI getLocationURI(HttpResponse response, HttpContext context) 
skorulis
  • 4,361
  • 6
  • 32
  • 43

2 Answers2

0

Have a look here: HttpClient 4 - how to capture last redirect URL

I would try getStatusLine() for start.

Community
  • 1
  • 1
Sebastian Roth
  • 11,344
  • 14
  • 61
  • 110
  • 3
    I saw that post. The method there seems to be for finding what redirects have happened after a request completes. When I try that method during a redirect I just get the initial URL that I entered. getStatusLine() simply gives me 200 OK – skorulis Nov 09 '10 at 22:27
0

My solution is to read location headers and follow them. This helped me:

if (statusCode != HttpStatus.SC_OK) {
    Header[] headers = response.getHeaders("Location");

    if (headers != null && headers.length != 0) {
        String newUrl = headers[headers.length - 1].getValue();
        // call again the same downloading method with new URL
        return downloadBitmap(newUrl);
    } else {
        return null;
    }
}

More in my post - Follow 302 redirects with AndroidHttpClient

Nikola
  • 144
  • 1
  • 3