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: [email protected]
For additional commands, e-mail: [email protected]


--------------------------------------------
<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




Reply via email to