views:

1969

answers:

1

I have rather simple HttpClient 4 code that calls HttpGet to get HTML output. The HTML returns with scripts and image locations all set to local (e.g. <img src="/images/foo.jpg"/>) so I need calling URL to make these into absolute (<img src="http://foo.com/images/foo.jpg"/&gt;) Now comes the problem - during the call there may be one or two 302 redirects so the original URL is no longer reflects the location of HTML.

How do I get the latest URL of the returned content given all the redirects I may (or may not) have?

I looked at HttpGet#getAllHeaders() and HttpResponse#getAllHeaders() - couldn't find anything.

Edited: HttpGet#getURI() returns original calling address

+3  A: 

That would be the current URL, which you can get by calling

  HttpGet#getURI();

EDIT: You didn't mention how you are doing redirect. That works for us because we handle the 302 ourselves.

Sounds like you are using DefaultRedirectHandler. We used to do that. It's kind of tricky to get the current URL. You need to use your own context. Here are the relevant code snippets,

        HttpGet httpget = new HttpGet(url);
        HttpContext context = new BasicHttpContext(); 
        HttpResponse response = httpClient.execute(httpget, context); 
        if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK)
            throw new IOException(response.getStatusLine().toString());
        HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute( 
                ExecutionContext.HTTP_REQUEST);
        HttpHost currentHost = (HttpHost)  context.getAttribute( 
                ExecutionContext.HTTP_TARGET_HOST);
        String currentUrl = currentHost.toURI() + currentReq.getURI();

The default redirect didn't work for us so we changed but I forgot what was the problem.

ZZ Coder
Alas, it will not - getURI() returns me the original calling URL
DroidIn.net
See my edit ..................
ZZ Coder
I don't do anything special - very basic HttpGet code. I google my problem I think I need to disable auto-redirect and "follow the trail" until I get 200
DroidIn.net
"Follow the trail" is much more flexible but it's not trivial. You have to watch for relative URL, circular redirect etc. If default redirect works for you, my code will get the URL for you.
ZZ Coder
Indeed - I ended up using currentHost.toURI() since I only need http://host part. Thank you for nailing this for me!
DroidIn.net