如何使用 IP 地址形成 GET HTTP 请求

How to form GET HTTP Request with IP Address

当我在 C 中使用下面的 GET HTTP 请求时,我不断收到 400 Bad Request 但当我使用字符串字符 IP 地址时它会起作用(例如:www.wwe.com/index .php):

GET %s HTTP/1.0\r\nHost: %s\r\nConnection: Keep-Alive\r\n\r\n

GET 之后的 %s 被替换为类似这样的内容:http://54.236.192.188/index.php 并且 Host 值由 hostent 结构的 h_name 字符串值提供。

所以一个请求看起来像这样:

GET http://54.236.192.188/index.php HTTP/1.0
Host: ec2-54-236-192-188.compute-1.amazonaws.com
Connection: Keep-Alive

我做错了什么?

GET的参数本身应该只是资源路径,Hostheader应该是请求所在机器的domain/host发送到,指定为原URL.

要请求 http://www.wwe.com/index.phpGET 请求将如下所示:

GET /index.php HTTP/1.1
Host: www.wwe.com
...

要请求 http://54.236.192.188/index.phpGET 请求将如下所示:

GET /index.php HTTP/1.1
Host: 54.236.192.188
...

但是,如果服务器在同一 IP 地址上运行多个网站,则您无法在 rquest 中提供 IP 地址。 Host header 告诉服务器要访问哪个特定网站。

除非需要,否则应使用HTTP/1.1而不是HTTP/1.0。例如,默认情况下 HTTP/1.0 不支持 Keep-AliveHost header 是 HTTP 1.1 所必需的。