On Tue, 2008-11-11 at 04:34 +0000, Amy de Buitléir wrote:
> Is there a way to find out what URL a request was redirected to? Here's the
> scenario:
> 
> 1. My code (client) does a GET http:/a/b/x
> 2. Server redirects to http://a/b/c/x (HttpClient handles the redirect)
> 3. Parsing the page, my code finds a link with a relative URL, "../y".
> 4. My code thinks the base URL is http:/a/b/x, so it resolves the partial
> URL in the link to http:/a/y and tries to fetch that page.
>     HOWEVER, the base URL is really http://a/b/c/x, so the link is really to
> http://a/b/y.
> 
> If there were some way to check for redirects, then my code could cope with
> them. When I was using HttpClient 3, I used to handle the redirects in my
> code, so I could continue to do that if necessary. However, HttpClient 4
> seems to handle redirects better than HttpClient 3 did, and I'd like to take
> advantage of that.
> 
> I believe I'm asking the same question as was asked here:
> http://mail-archives.apache.org/mod_mbox/hc-httpclient-users/200810.mbox/browser.
> I have read the replies to that post, however, I don't think those replies
> really addressed the scenario. If there isn't currently a way to find out
> the redirected URL, then perhaps I should enter a feature request. Thanks in
> advance for any suggestions.



To get final request location:
--------
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("https://localhost/";);

HttpContext context = new BasicHttpContext();
HttpResponse response = httpClient.execute(httpget, context);
HttpEntity entity = response.getEntity();
if (entity != null) {
    entity.consumeContent();
}
HttpUriRequest request = (HttpUriRequest) context.getAttribute(
        ExecutionContext.HTTP_REQUEST);

System.out.println(request.getURI());
--------

To get all intermediate redirect locations:
--------
DefaultHttpClient httpClient = new DefaultHttpClient();

httpClient.setRedirectHandler(new DefaultRedirectHandler() {

    @Override
    public URI getLocationURI(HttpResponse response, HttpContext
context) throws ProtocolException {
        URI uri = super.getLocationURI(response, context);
        System.out.println("redirect - > " + uri);
        return uri; 
    }
    
});
        
HttpGet httpget = new HttpGet("https://localhost/";);
HttpResponse response = httpClient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
    entity.consumeContent();
}
--------

Hope this helps

Oleg


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to