You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Amy de Buitléir <am...@nualeargais.ie> on 2008/11/11 05:34:45 UTC
Finding out the URL after a redirect
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.
Re: Finding out the URL after a redirect
Posted by Amy de Buitléir <am...@nualeargais.ie>.
Thank you so much for your sample code, Oleg. I tried it, and it solves my
problem perfectly.
Re: Finding out the URL after a redirect
Posted by Oleg Kalnichevski <ol...@apache.org>.
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: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org
Re: Finding out the URL after a redirect
Posted by sebb <se...@gmail.com>.
On 11/11/2008, Amy de Buitléir <am...@nualeargais.ie> 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.
>
In HttpClient 3.1, one can use
httpMethod.getURI()
after the event to get the updated URI.
Not sure about HC 4.x, but it may well behave the same - try it and see.
---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org