简体   繁体   English

Java HTTP响应代码200,而应为301

[英]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.

相关问题 Android:检查HTTP响应码:得到200; 应该是 302 - Android: Checking HTTP response code: getting 200; should be 302 执行 HTTP 时获得 301 使用 Java 获得呼叫,但使用 Postman 执行相同操作时获得 200 - Getting 301 while doing HTTP get call using Java but getting 200 while doing same using Postman Java HTTPS POST 请求返回 200 的响应代码,而我知道它应该返回 302 的响应代码 - Java HTTPS POST Request Is Returning A Response Code Of 200 When I Know It Should Be Returning A Response Code Of 302 Android HttpURLConnection接收HTTP 301响应代码 - Android HttpURLConnection receives HTTP 301 response code Java中的HTTP响应代码 - HTTP response code in Java Java:Android:Retrofit 2响应代码为200,response.body为null - Java : Android : Retrofit 2 response code is 200, response.body is null 我在 Retrofit 中得到了奇怪的响应,协议=http/1.1,代码=200 - I get strange response in Retrofit with protocol=http/1.1, code=200 有没有一种确定Java响应是否在200系列中的规范方法? - Is there a canonical way for determining if an HTTP response is in the 200 series in Java? GET方法被调用两次,代码为301和200 - GET method called twice with code 301 and 200 生成Http 301响应消息 - Generating Http 301 Response Message
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM