简体   繁体   English

HTTP套接字:响应标头

[英]HTTP Sockets: Response Headers

I'm practising HTTP using Sockets in C# and i'm able to get the response body with this code: 我正在使用C#中的套接字练习HTTP,我可以使用以下代码获取响应正文:

int bytes = 0;
while (bytes > 0)
{
    bytes = HttpSocket.Receive(RecieveBytes, RecieveBytes.Length, 0);
    Response.Body = Response.Body + Encoding.ASCII.GetString(RecieveBytes, 0, bytes);
}

When I read Response.Body it only contains HTML code. 当我阅读Response.Body时,它只包含HTML代码。 (The body). (身体)。

How would I get more information? 我如何获得更多信息? Like the response headers? 像响应头一样? I already did some googling but there's not clear answer. 我已经做了一些谷歌搜索,但没有明确的答案。

Note: I know there's not point int creating a class or framework for HTTP in C#, since we have HttpWebRequest. 注意:我知道在C#中创建HTTP的类或框架没有意义,因为我们有HttpWebRequest。 However I'm doing this for learning purposes. 但是我这样做是为了学习目的。

Swen 斯文

It can't be true that you're only receiving the HTTP Response body, Socket.Receive() will receive all available bytes that the server has sent, so the response headers must be there too (if you are talking to a common web server and have issued a valid request). 您只接收HTTP响应主体并不是真的, Socket.Receive()将接收服务器发送的所有可用字节,因此响应头也必须在那里(如果您正在与一个公共Web通信)服务器并已发出有效请求)。 Use a tool like Fiddler to monitor the request-response pair and check that the response starts with HTTP/1.1 200 OK . 使用像Fiddler这样的工具来监视请求 - 响应对,并检查响应是否以HTTP/1.1 200 OK

You can call Receive() until you've received a double \\r\\n , which indicates all headers have been sent. 您可以调用Receive()直到收到双\\r\\n ,表示已发送所有标头。 From there on, you'll have to implement RFC 2616 section 4.4 to determine how much (if any) message body data to read. 从那时起,您必须实现RFC 2616第4.4节以确定要读取多少(如果有)消息正文数据。

The most common is a Content-length response header, which indicates how many bytes you should read after the double \\r\\n . 最常见的是Content-length响应头,它指示在double \\r\\n之后应该读取多少字节。 If that header isn't there (or if your request method was HEAD , in which case you should stop reading since there won't be a message body) and none of the other message length cases apply, you're done reading data at this point and you should not try to Receive() any more data. 如果那个标题不存在(或者你的请求方法是HEAD ,在这种情况下你应该停止阅读,因为没有消息体)并且没有其他消息长度情况适用,你已经完成了读取数据这一点,你不应该尝试Receive()任何更多的数据。

But if it's for learning purposes, you should have found the RFC and implemented that. 但如果它是出于学习目的,你应该找到RFC并实现它。 You can't just go read data and hope it'll come in fine, that's what protocols are for. 你不能只是去阅读数据并希望它会好起来,这就是协议的用途。

你的while循环永远不会执行,因为'bytes'最初为零,只有在它为正时才循环。

This is not the best way to handle HTTP. 这不是处理HTTP的最佳方式。 The Socket class operates on raw bytes, but HTTP headers operate on lines of text instead. Socket类对原始字节进行操作,但HTTP头对文本行进行操作。 You need to read lines of text in a loop until a blank line is encountered, then process the headers to decide how to finish reading the remaining raw bytes, if any. 您需要在循环中读取文本行,直到遇到空白行,然后处理标题以决定如何完成读取剩余的原始字节(如果有)。 Have a look at the NetworkStream and StreamReader classes for that. 看看NetworkStreamStreamReader类。 A better option is to use the HttpClient client class instead, and let it handle all of these details for you. 更好的选择是使用HttpClient客户端类,让它为您处理所有这些细节。

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

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