Redirect with Curl / HttpClient: "Location" header missing

when i get the following url with curl

curl -D headers.http "http://www.springerlink.com/index/10.1007/s00453-007-9157-8"

      

the headers.http file contains the "Location" header:

HTTP/1.1 302 Found
Date: Tue, 27 Oct 2009 17:00:20 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Location: http://www.springerlink.com/link.asp?id=c104731297q64224
Set-Cookie: CookiesSupported=True; expires=Wed, 27-Oct-2010 17:00:20 GMT; path=/
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Length: 173

      

but when i used apache httpclient library this "Location:" header was missing (?).

int status = httpClient.executeMethod(method);
if(status!=HttpStatus.SC_OK &&
status!=HttpStatus.SC_MOVED_TEMPORARILY &&
status!=HttpStatus.SC_MOVED_PERMANENTLY
    )
    {
    throw new IOException("connection failure for "+url+" status:"+status);
    }
Header header=method.getResponseHeader("Location");
if(header==null )
    {

    for(Header h:method.getResponseHeaders())
        {
        LOG.info(h.toString());
        }

    throw new IOException(
        "Expected a redirect for "+url
        );
    }

      

I have listed the headers below:

INFO: Date: Tue, 27 Oct 2009 17:05:13 GMT
INFO: Server: Microsoft-IIS/6.0
INFO: X-Powered-By: ASP.NET
INFO: X-AspNet-Version: 2.0.50727
INFO: Set-Cookie: ASP.NET_SessionId=js1o5wqnuhuh24islnvkyr45; path=/; HttpOnly
INFO: Cache-Control: private
INFO: Content-Type: text/html; charset=utf-8
INFO: Content-Length: 17245

      

uhh ???

+2


source to share


3 answers


What happens to you with curl

a 302 , which is actually a redirect to the url in the location header.

Using Apache httpclient, it does the redirect for you and returns the headers from the request to the redirected location.

To demonstrate this attempt



curl -D headers.http "http://www.springerlink.com/link.asp?id=c104731297q64224"

      

and compare the answer.

edit: there are actually about 4 redirects if you follow each location header with curl.

+2


source


http://www.springerlink.com/index/10.1007/s00453-007-9157-8 is actually a redirect. Since the parameter -D

means "headers only", the first is not redirected to the specified one Location: ...

, but the second. Take a look at Content-Length, it is very different.



What happens if you don't use -D

?

0


source


Add this

  method.setFollowRedirects(false); 

      

Before executing the method.

HttpClient does the default redirect automatically, but Curl does not.

0


source







All Articles