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 swatkatz <mo...@gmail.com> on 2010/04/28 17:45:22 UTC

Distinguish between temporary and permanent redirects.

Hello,

Using HttpClient 4.0. How do I differentiate between a temporary redirect
and a permanent redirect ? I am getting the client to follow the redirects
automatically and I get the new URL once I read the final response but how
do I know if the client was temporarily redirected to this URL or whether
the redirection was permanent ? The status code outside seems to be 200, so
I can't tell what the intermediate status codes were.

Thanks in advance.
-- 
View this message in context: http://old.nabble.com/Distinguish-between-temporary-and-permanent-redirects.-tp28390385p28390385.html
Sent from the HttpClient-User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: Distinguish between temporary and permanent redirects.

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Wed, 2010-04-28 at 10:45 -0700, swatkatz wrote:
> That's the first thing I looked at - it doesn't tell me whether a URL was
> temporarily or permanently redirected. I have to take different actions
> based on whether the URL was a temporary or permanent redirect.

Then you obviously need to put together a custom redirect strategy.

Oleg



---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: Distinguish between temporary and permanent redirects.

Posted by swatkatz <mo...@gmail.com>.
That's the first thing I looked at - it doesn't tell me whether a URL was
temporarily or permanently redirected. I have to take different actions
based on whether the URL was a temporary or permanent redirect.
-- 
View this message in context: http://old.nabble.com/Distinguish-between-temporary-and-permanent-redirects.-tp28390385p28391903.html
Sent from the HttpClient-User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: Distinguish between temporary and permanent redirects.

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Wed, 2010-04-28 at 09:48 -0700, swatkatz wrote:
> Thanks. I was about to this same thing except more complicated because I
> think RedirectHandler is deprecated. I think having this information
> accessible from the context would be great. Perhaps I should put it in JIRA
> ?

You can obtain an instance of RedirectLocations from the context with
'http.protocol.redirect-locations' attribute key to examine intermediate
redirect URIs.

Oleg


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: Distinguish between temporary and permanent redirects.

Posted by swatkatz <mo...@gmail.com>.
Thanks. I was about to this same thing except more complicated because I
think RedirectHandler is deprecated. I think having this information
accessible from the context would be great. Perhaps I should put it in JIRA
?
-- 
View this message in context: http://old.nabble.com/Distinguish-between-temporary-and-permanent-redirects.-tp28390385p28391163.html
Sent from the HttpClient-User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: Distinguish between temporary and permanent redirects.

Posted by Ken Krugler <kk...@transpac.com>.
There's probably a better way, but in the Bixo code I added a  
RedirectHandler that records the last permanent redirect (as well as  
total number of redirects):

     // Keys used to access data in the Http execution context.
	private static final String PERM_REDIRECT_CONTEXT_KEY = "perm- 
redirect";
	private static final String REDIRECT_COUNT_CONTEXT_KEY = "redirect- 
count";

     private static class MyRedirectHandler extends  
DefaultRedirectHandler {
     	
     	@Override
     	public URI getLocationURI(HttpResponse response, HttpContext  
context) throws ProtocolException {
     	    URI result = super.getLocationURI(response, context);

     	    int statusCode = response.getStatusLine().getStatusCode();
     	    if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY) {
     	        context.setAttribute(PERM_REDIRECT_CONTEXT_KEY, result);
     	    }

     	    // Keep track of the number of redirects.
     	    Integer count =  
(Integer)context.getAttribute(REDIRECT_COUNT_CONTEXT_KEY);
     	    if (count == null) {
     	        count = new Integer(0);
     	    }

     	    count += 1;
     	    context.setAttribute(REDIRECT_COUNT_CONTEXT_KEY, count);

     	    return result;
     	}
     }

and then

             _httpClient.setRedirectHandler(new MyRedirectHandler());

and finally (with some code deleted)

             HttpContext localContext = new BasicHttpContext();
             response = _httpClient.execute(request, localContext);

             URI permRedirectUri =  
(URI)localContext.getAttribute(PERM_REDIRECT_CONTEXT_KEY);
             if (permRedirectUri != null) {
                 newBaseUrl = permRedirectUri.toURL().toExternalForm();
             }

-- Ken

On Apr 28, 2010, at 8:45am, swatkatz wrote:

>
> Hello,
>
> Using HttpClient 4.0. How do I differentiate between a temporary  
> redirect
> and a permanent redirect ? I am getting the client to follow the  
> redirects
> automatically and I get the new URL once I read the final response  
> but how
> do I know if the client was temporarily redirected to this URL or  
> whether
> the redirection was permanent ? The status code outside seems to be  
> 200, so
> I can't tell what the intermediate status codes were.
>
> Thanks in advance.
> -- 
> View this message in context: http://old.nabble.com/Distinguish-between-temporary-and-permanent-redirects.-tp28390385p28390385.html
> Sent from the HttpClient-User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>

--------------------------------------------
<http://ken-blog.krugler.org>
+1 530-265-2225






--------------------------------------------
Ken Krugler
+1 530-210-6378
http://bixolabs.com
e l a s t i c   w e b   m i n i n g