简体   繁体   English

Java套接字编程-HTTP 1.1出现301错误

[英]Java Socket Programming - 301 Error with HTTP 1.1

I just start learning socket programming with Java and I already encountered an unusual behavior. 我刚刚开始学习Java套接字编程,并且已经遇到了异常行为。 Here's the code snippet 这是代码片段

writer.println("GET " + path + " " + protocol);
//writer.println();
writer.println("Host: " + hostname);
writer.println();
writer.flush();

This will give me "301 Moved Permanently" code with both HTTP 1.1 and 1.0. 这将为我提供HTTP 1.1和1.0的“ 301永久移动”代码。 If I uncomment the empty line between the request and host name 如果我取消注释请求和主机名之间的空行

writer.println("GET " + path + " " + protocol);
writer.println();
writer.println("Host: " + hostname);
writer.println();
writer.flush();

It would give me "HTTP/1.1 400 Bad Request" for HTTP 1.1 and "HTTP/1.1 200 OK" for HTTP 1.0. 对于HTTP 1.1,它会给我“ HTTP / 1.1 400错误请求”,对于HTTP 1.0,它会给我“ HTTP / 1.1 200 OK”

Why does it have such behavior? 为什么会有这种行为? Does this happen because we have the request in HTTP 1.0 and the response is in HTTP 1.1? 发生这种情况是因为我们在HTTP 1.0中有请求,而响应在HTTP 1.1中?

Thanks. 谢谢。

This will give me "301 Moved Permanently" code with both HTTP 1.1 and 1.0. 这将为我提供HTTP 1.1和1.0的“ 301永久移动”代码。

HTTP status code 301 is a redirect to a new URL: HTTP状态代码301是到新URL的重定向:

The requested resource has been assigned a new permanent URI and any future references to this resource SHOULD use one of the returned URIs. 所请求的资源已被分配了一个新的永久URI,以后对该资源的任何引用都应使用返回的URI之一。 Clients with link editing capabilities ought to automatically re-link references to the Request-URI to one or more of the new references returned by the server, where possible. 具有链接编辑功能的客户端应该在可能的情况下自动将对Request-URI的引用重新链接到服务器返回的一个或多个新引用。 This response is cacheable unless indicated otherwise. 除非另有说明,否则此响应是可缓存的。

The new permanent URI SHOULD be given by the Location field in the response. 新的永久URI应该由响应中的Location字段给出。 Unless the request method was HEAD, the entity of the response SHOULD contain a short hypertext note with a hyperlink to the new URI(s). 除非请求方法是HEAD,否则响应的实体应该包含简短的超文本注释,并带有指向新URI的超链接。

If the 301 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued. 如果接收到响应GET或HEAD以外的请求的301状态代码,则用户代理不得自动重定向该请求,除非用户可以确认,因为这可能会更改发出该请求的条件。

Note: When automatically redirecting a POST request after receiving a 301 status code, some existing HTTP/1.0 user agents will erroneously change it into a GET request. 注意:当收到301状态代码后自动重定向POST请求时,某些现有的HTTP / 1.0用户代理将错误地将其更改为GET请求。

The server is telling you that the URL you sent your GET request to is no longer valid. 服务器告诉您,您将GET请求发送到的URL不再有效。 You need to extract the value of the Location header from the server's response and then repeat the same request to the specified URL. 您需要从服务器的响应中提取Location标头的值,然后对指定的URL重复相同的请求。

It would give me "HTTP/1.1 400 Bad Request" for HTTP 1.1 and "HTTP/1.1 200 OK" for HTTP 1.0. 对于HTTP 1.1,它会给我“ HTTP / 1.1 400错误请求”,对于HTTP 1.0,它会给我“ HTTP / 1.1 200 OK”。

Why does it have such behavior? 为什么会有这种行为? Does this happen because we have the request in HTTP 1.0 and the response is in HTTP 1.1? 发生这种情况是因为我们在HTTP 1.0中有请求,而响应在HTTP 1.1中?

The Host header is optional in HTTP 1.0 but is required in HTTP 1.1: Host标头在HTTP 1.0中是可选的 ,但在HTTP 1.1中是必需的

A client MUST include a Host header field in all HTTP/1.1 request messages. 客户端必须在所有HTTP / 1.1请求消息中包含主机标头字段。 If the requested URI does not include an Internet host name for the service being requested, then the Host header field MUST be given with an empty value. 如果所请求的URI不包含所请求服务的Internet主机名,则“主机头”字段必须给出一个空值。 An HTTP/1.1 proxy MUST ensure that any request message it forwards does contain an appropriate Host header field that identifies the service being requested by the proxy. HTTP / 1.1代理务必确保其转发的任何请求消息均包含适当的Host标头字段,该字段标识由代理请求的服务。 All Internet-based HTTP/1.1 servers MUST respond with a 400 (Bad Request) status code to any HTTP/1.1 request message which lacks a Host header field. 所有基于Internet的HTTP / 1.1服务器必须对任何缺少主机头字段的HTTP / 1.1请求消息以400(错误请求)状态码进行响应。

So, when you do not insert the extra blank line, you end up sending these requests separately: 因此,当您不插入多余的空白行时,最终将分别发送这些请求:

GET /path HTTP/1.0
Host: hostname

GET /path HTTP/1.1
Host: hostname

Which are both valid. 两者都有效。

But, when you insert the extra blank line, you are actually sending two separate requests at one time: 但是,当您插入多余的空行时,实际上是一次发送两个单独的请求:

GET /path HTTP/1.x;

Host: hostname

The request headers and request body are separated by a blank line, and a GET request does not have a request body, so the first blank line ends the request. 请求标头和请求正文由空行分隔,并且GET请求没有请求正文,因此第一个空行结束了请求。

So, in this case, the first request is valid only for HTTP 1.0 and is invalid for HTTP 1.1 because the Host header is missing. 因此,在这种情况下,第一个请求仅对HTTP 1.0有效,而对HTTP 1.1无效,因为缺少Host头。 The second request is just plain invalid in either version. 在任何一个版本中,第二个请求都是无效的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM