2017-03-04 82 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 
相關問題