2017-03-04 77 views
0

我试图发送一个HEAD请求这个网址:400 HTTP/1.1,但不是在HTTP错误的请求/ 1.0

http://ubuntu-releases.mirror.net.in/releases/16.04.2/ubuntu-16.04.2-desktop-amd64.iso

及获取文件的大小。我现在头请求如下所示:

head_request = "HEAD " + file_path + " HTTP/1.0%s" % ('\r\n\r\n') 
socket.socket(socket.AF_INET, socket.SOCK_STREAM).send(head_request) 

其中file_path"/releases/16.04.2/ubuntu-16.04.2-desktop-amd64.iso"。这很好,但是当我用1.1取代1.0时,我得到了400 HTTP Bad Request

head_request = "HEAD " + file_path + " HTTP/1.1%s" % ('\r\n\r\n') 

为什么会发生这种情况?

回答

2

在HTTP/1.1,必须提供Host:报头。

示范使用netcatnc)实用程序:

$ nc ubuntu-releases.mirror.net.in 80 <<END 
HEAD /releases/16.04.2/ubuntu-16.04.2-desktop-amd64.iso HTTP/1.0 

END 
HTTP/1.1 200 OK 
Date: Sat, 04 Mar 2017 07:25:22 GMT 
Server: Apache/2.4.18 (Ubuntu) 
Last-Modified: Wed, 15 Feb 2017 21:44:24 GMT 
ETag: "5ca30000-5489895805e00" 
Accept-Ranges: bytes 
Content-Length: 1554186240 
Connection: close 
Content-Type: application/x-iso9660-image 

$ nc ubuntu-releases.mirror.net.in 80 <<END 
HEAD /releases/16.04.2/ubuntu-16.04.2-desktop-amd64.iso HTTP/1.1 

END 

HTTP/1.1 400 Bad Request 
Date: Sat, 04 Mar 2017 07:25:33 GMT 
Server: Apache/2.4.18 (Ubuntu) 
Connection: close 
Content-Type: text/html; charset=iso-8859-1 

$ nc ubuntu-releases.mirror.net.in 80 <<END 
HEAD /releases/16.04.2/ubuntu-16.04.2-desktop-amd64.iso HTTP/1.1 
Host: ubuntu-releases.mirror.net.in 

END 

HTTP/1.1 200 OK 
Date: Sat, 04 Mar 2017 07:27:27 GMT 
Server: Apache/2.4.18 (Ubuntu) 
Last-Modified: Wed, 15 Feb 2017 21:44:24 GMT 
ETag: "5ca30000-5489895805e00" 
Accept-Ranges: bytes 
Content-Length: 1554186240 
Content-Type: application/x-iso9660-image 
相关问题