http请求没有收到全部信息 - C. [英] http request doesn't receive the whole information - C
问题描述
连接到服务器后,我需要使用 GET
命令从中获取所有信息。由于某种原因,我不允许在我的get命令之后输入 \\\\ n
,所以我发送到服务器的命令就像获取http://somethingrandom.com HTTP / 1.0
。
After connecting to a server, I need to use the GET
command to get all the information from it. I am, for some reason, not allowed to put \r\n
after my get command, so the command I'm sending to the server is something like GET http://somethingrandom.com HTTP/1.0
.
将请求发送到地址后,我收不到来自终端的正常 GET
的输出相同:
After sending the request to the address, I do not receive the same output that a normal GET
from a terminal would:
更具体地说,我收到以下信息:
To be more specific, I receive the following information:
- http状态
- 一些奇怪的数据:
- 日期
- 服务器
- Last-Modified
- ETag
- Accept-Ranges
- 内容长度
- 不同
- 连接
- Content-Type
- The http status
- Some odd data:
- Date
- Server
- Last-Modified
- ETag
- Accept-Ranges
- Content-Length
- Vary
- Connection
- Content-Type
我认为
\ r \\ n的角色Ñ
是准确地忽略该信息。 (在此示例中,额外的< head>
标记信息)。如果它与我的代码有关(最有可能)我将在之后提供它。I think that the role of
\r\n
is to exactly ignore that information. (In this example the extra<head>
tag information). If it has something to do with my code(most probably) I'll provide it afterwards.推荐答案
你使用的第一个命令在终端上实际上是程序
GET
,它使用Perl LWP库执行正确的HTTP请求并返回响应。您给出的命令末尾的HTTP / 1.0
实际上被忽略,因为只需要URL。因此GET
类似于curl
或wget
。您甚至可以使用GET --help
来确切使用。The first command you use on the terminal is actually the program
GET
which does a proper HTTP request using the Perl LWP library and gives you the response back. TheHTTP/1.0
at the end of the command you gave is actually ignored because only the URL is expected. ThusGET
is similar tocurl
orwget
. You can even callGET --help
for the exact usage.您在C中尝试做的事情是在没有任何库的情况下处理HTTP。在这种情况下,您必须正确读取和解析响应,即
GET
,curl
或wget
如果您使用这些命令,命令会为您执行。要正确执行此操作,您需要首先了解HTTP协议。虽然HTTP / 1.0不是最新的标准,但我建议首先研究它,因为它是最简单的,它仍然被Web服务器接受。有关标准,请参见 RFC 1945 。The thing you are trying do in C is to deal with HTTP without any library. In this case you have to properly read and parse the response, i.e. exactly the thing which
GET
,curl
orwget
commands do for you if you use these commands. To do this properly you need to understand the HTTP protocol first. While HTTP/1.0 is not the latest standard I recommend to study this first because it is the simplest one and it is still accepted by web servers. See RFC 1945 for the standard.从阅读标准中你会看到,有一个HTTP响应标题和正文,由
\\\\ n
分隔。因此,您需要从标题中获取信息以正确解释正文。在最简单的情况下,您可以删除标题。From reading the standard you will see, that there is a HTTP response header and body, delimited by
\r\n
. Thus, you need to take the information from the header in order to interpret the body properly. In the simplest case you can just strip the header.这篇关于http请求没有收到全部信息 - C.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!