[英]Java HTTP response code 200, while it should be 301
The following code is driving me insane. 以下代码使我发疯。 It sometimes returns the wrong response code and I do not understand why.
有时返回错误的响应代码,我不明白为什么。 Eg,
http://www.ebay.com/itm/131709867498
returns a 301 Moved Permanently
in real life (Chrome and Edge), while the following code returns 200
. 例如,
http://www.ebay.com/itm/131709867498
: 301 Moved Permanently
返回301 Moved Permanently
(在现实生活中301 Moved Permanently
)(Chrome和Edge),而以下代码返回200
。 Thanks a ton in advance. 在此先感谢一吨。 (In case it matters, I am running Java 1.8.0_66.)
(以防万一,我正在运行Java 1.8.0_66。)
HttpURLConnection connection = (HttpURLConnection)new URL("http://www.ebay.com/itm/131709867498").openConnection();
connection.setRequestMethod("HEAD");
connection.setInstanceFollowRedirects(false);
int statusCode = connection.getResponseCode();
System.out.println(statusCode);
When I run your URL in Curl I get a '200'. 当我在Curl中运行您的URL时,我得到一个“ 200”。 Why do you think it should be returning 301?
您为什么认为它应该返回301?
url -I "http://www.ebay.com/itm/131709867498"
HTTP/1.1 200 OK
X-EBAY-C-REQUEST-ID: ri=yGq%2BS7ChLgXP,rci=XOZjR%2BAfGDb5BkY5
RlogId: t6ulcpjqcj9%3Feog4d71f%2B66%600-15436278e5c-0x17a
X-Frame-Options: SAMEORIGIN
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Content-Type: text/html;charset=UTF-8
Content-Language: en-US
Content-Length: 0
Server: Server
Date: Thu, 21 Apr 2016 00:10:42 GMT
Connection: keep-alive
Set-Cookie: JSESSIONID=4694AD028535546045A4E2853D7F8B72; Path=/; HttpOnly
Set-Cookie: ebay=%5Esbf%3D%23%5E;Domain=.ebay.com;Path=/
Set-Cookie: dp1=bu1p/QEBfX0BAX19AQA**58f94e02^bl/US5ada8182^;Domain=.ebay.com;Expires=Sat, 21-Apr-2018 00:10:42 GMT;Path=/
Set-Cookie: s=CgAD4ACBXGWwCMzYyNzhlNTcxNTQwYTYyMjg3MDFkNTExZmZiN2MyMGKrI6lP;Domain=.ebay.com;Path=/; HttpOnly
Set-Cookie: nonsession=CgADLAAFXGCGKMQDKACBgfhwCMzYyNzhlNTcxNTQwYTYyMjg3MDFkNTExZmZiN2MyMGIcCAX2;Domain=.ebay.com;Expires=Fri, 21-Apr-2017 00:10:42 GMT;Path=/
You are using "Head" method but Browsers use "GET" method, if you change 您正在使用“ Head”方法,但是浏览器使用“ GET”方法(如果您进行更改)
connection.setRequestMethod("HEAD");
TO 至
connection.setRequestMethod("GET");
you will see 301 status code 您将看到301状态码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.