hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ken Krugler <kkrugler_li...@transpac.com>
Subject Re: Last redirect URL
Date Wed, 30 Sep 2009 22:32:00 GMT
Hi all,

Just to log it someplace public, here's what I wound up doing to get  
the final URL (probably overkill re all of the to/from URI/URL stuff):

             getter = new HttpGet(new URI(url));
             HttpContext localContext = new BasicHttpContext();
             response = _httpClient.execute(getter, localContext);

             int httpStatus = response.getStatusLine().getStatusCode();
             if (httpStatus != HttpStatus.SC_OK) {
                 throw new HttpFetchException(url, "Error fetching " +  
url, httpStatus, headerMap);
             }

             HttpHost host =  
(HttpHost)localContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
             HttpUriRequest finalRequest =  
(HttpUriRequest 
)localContext.getAttribute(ExecutionContext.HTTP_REQUEST);

             try {
                 URL hostUrl = new URI(host.toURI()).toURL();
                 redirectedUrl = new URL(hostUrl,  
finalRequest.getURI().toString()).toExternalForm();
             } catch (MalformedURLException e) {
                 LOGGER.warn("Invalid host/uri specified in final  
fetch: " + host + finalRequest.getURI());
                 redirectedUrl = url;
             }

> This is only partially correct. The original request object remains  
> unmodified.
> So, one needs to retrieve the internal HttpUriRequest and HttpHost  
> objects from
> the execution context in order to find out the final request URI /  
> target host.
> For details see:
>
> http://hc.apache.org/httpcomponents-client/tutorial/html/fundamentals.html#d4e205

The key bit of documentation was at:

http://hc.apache.org/httpcomponents-client/tutorial/html/httpagent.html#d4e1022

-- Ken


--------------------------
Ken Krugler
TransPac Software, Inc.
<http://www.transpac.com>
+1 530-210-6378


Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message