컬/httpclient로 리디렉션 : 헤더 "위치"누락
문제
다음 URL을 얻을 때 곱슬 곱슬하다
curl -D headers.http "http://www.springerlink.com/index/10.1007/s00453-007-9157-8"
파일 headers.http에는 "위치"헤더가 포함되어 있습니다.
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 라이브러리를 사용했을 때이 "위치 :"헤더가 누락되었습니다 (?).
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"
응답을 비교하십시오.
편집 : 컬로 각 위치 헤더를 따라 가면 실제로 약 4 개의 리디렉션이 있습니다.
다른 팁
http://www.springerlink.com/index/10.1007/S00453-007-9157-8 실제로 리디렉션입니다. 이후 -D
옵션은 "헤더 만"을 의미하며 첫 번째는 지정된 것과 리디렉션되지 않습니다. Location: ...
, 두 번째는입니다. 컨텐츠 길이를 살펴보면 크게 다릅니다.
당신이 떠날 때 어떻게 되는가 -D
?
이거 추가 해봐
method.setFollowRedirects(false);
메소드를 실행하기 전에.
httpclient는 기본적으로 자동으로 리디렉션을 따릅니다.
제휴하지 않습니다 StackOverflow