Curl / HttpClientでリダイレクト:header“ Location”行方不明
質問
カールで次のURLを取得したとき
curl -D headers.http "http://www.springerlink.com/index/10.1007/s00453-007-9157-8"
ファイルheaders.httpには" Location"が含まれていますヘッダー:
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
しかし、apache httpclientライブラリを使用した場合、この" Location:"ヘッダーがありません(?)。
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
);
}
以下のヘッダーをリストしました:
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
ええと??????
解決
何が起こっているかは、 curl
で、 302 は実際には場所ヘッダーのURLへのリダイレクトです。
Apache httpclientを使用すると、リダイレクトが行われ、リクエストからヘッダーがリダイレクト先に返されます。
これを試すには
curl -D headers.http "http://www.springerlink.com/link.asp?id=c104731297q64224"
応答を比較します。
編集:各場所のヘッダーをcurlでたどると、実際には約4つのリダイレクトがあります。
他のヒント
http://www.springerlink.com/index/10.1007 / s00453-007-9157-8 は実際にはリダイレクトです。 -D
オプションは「ヘッダーのみ」を意味するため、最初のオプションは指定された Location:...
にリダイレクトされませんが、2番目のオプションはリダイレクトされます。 Content-Lengthを見てください。これはかなり異なります。
-D
を省略するとどうなりますか?
これを追加
method.setFollowRedirects(false);
メソッドを実行する前。
HttpClientはデフォルトで自動的にリダイレクトに従いますが、Curlはそうではありません。
所属していません StackOverflow