简体   繁体   English

空的http响应正文

[英]Empty http response body

I am trying to make HTTP request/response using sockets in C#. 我正在尝试使用C#中的套接字进行HTTP请求/响应。 GET request appears below. GET请求出现在下面。

 StringBuilder sb = new StringBuilder();
 sb.AppendFormat("GET http://{0}/ HTTP/1.1\r\n", hostname);
 sb.AppendFormat("Host: {0}\r\n", hostname);
 sb.Append("Connection: keep-alive\r\n");
 sb.Append(@"Accept:text/html,*/*");
 sb.Append("\r\n\r\n");

where hostname is something like 'mit.edu' or 'facebook.com' or 'google.com' or anything else. 其中的主机名类似于“ mit.edu”或“ facebook.com”或“ google.com”或其他任何名称。 For some strange reason I have just a status-line (with 200 status code) and headers as http response. 由于某些奇怪的原因,我只有一条状态行(带有200个状态代码)和标头作为http响应。 But there is no message body in the response: attached srceenshot of my console app 但是响应中没有消息正文: 控制台应用程序的附加srceenshot

Here is a method that perform all manipulations with socket and make http request: 这是一种使用套接字执行所有操作并发出http请求的方法:

public static void DoHttpGetRequest(String hostname, Int16 port = 80) {
      IPHostEntry ipHostEntry = Dns.GetHostEntry(hostname);
      IPAddress ipAdress = ipHostEntry.AddressList[0];
      IPEndPoint ipEndPoint = new IPEndPoint(ipAdress, port);

      Socket socket = new Socket(ipAdress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

      socket.Connect(ipEndPoint);

      String request = CreateRequest(hostname);

      Byte[] byteRequest = Encoding.UTF8.GetBytes(request);
      Byte[] byteResponse = new Byte[1000];

      int bytesSent = socket.Send(byteRequest);
      int bytesReceive = socket.Receive(byteResponse);

      Console.WriteLine(request);
      Console.WriteLine();
      Console.WriteLine(Encoding.UTF8.GetString(byteResponse, 0, bytesReceive));

      socket.Shutdown(SocketShutdown.Both);
      socket.Close();
}

My first thought was that the socket hadn't received the whole response from the server. 我首先想到的是套接字没有收到服务器的全部响应。 In this case I do still not know how to solve the problem. 在这种情况下,我仍然不知道如何解决问题。

So what is going on? 那么发生了什么? Where is the mistake? 错误在哪里?

Don't expect to get the full response inside a single receive. 不要期望在一次接收中得到完整的响应。 What you do is to receive data until you have the full response header (this could take several receive calls too), then parse the header to find out how long the response is and then read the necessary data of the response which also can need multiple receive calls. 您要做的是接收数据,直到拥有完整的响应标头为止(这也可能需要几个接收调用),然后解析标头以找出响应有多长时间,然后读取响应的必要数据,这也可能需要多个接听电话。 And since you are doing a HTTP/1.1 request you also have to deal with chunked responses. 而且由于您正在执行HTTP / 1.1请求,所以您还必须处理分块响应。

I recommend to better use a HTTP library to handle all the problems. 我建议更好地使用HTTP库来处理所有问题。 If you insist on doing it all by your own read the specification of HTTP and implemented accordingly. 如果您坚持要自己做所有事情,请阅读HTTP规范并相应地实现。 It also helps to look around at stackoverflow for similar requests because this problem you have is very typical for someone trying to implement HTTP first time, without understanding enough on how sockets, TCP and HTTP work. 它也有助于环顾类似请求的stackoverflow ,因为这个问题对于第一次尝试实现HTTP的人来说非常典型,而对于套接字,TCP和HTTP的工作原理却不够了解。

I think you should keep the socket open for a longer time. 我认为您应该将插座打开更长的时间。 Insert a 插入一个

Thread.Wait(5000);

may help. 可能会有所帮助。 Then you can do a second socket.Receive as Steffen proposed. 然后你可以做第二个socket.Steffen建议接收。 Maybe you can try the following (wait until the server closed the connection): 也许您可以尝试以下方法(等待服务器关闭连接):

while (socket.Connected) do
{
    int bytesReceive = socket.Receive(byteResponse);
}

But I didn't test this. 但是我没有测试。 It's easier to use the System.Net.Http.HttpClient 使用System.Net.Http.HttpClient更容易

Everything I need to do is to receive data with multiple receive calls until all response information received. 我要做的就是通过多个接收呼叫接收数据,直到接收到所有响应信息为止。

do
{                
     bytesReceive = socket.Receive(byteResponse, byteResponse.Length, 0);
     response += Encoding.UTF8.GetString(byteResponse, 0, bytesReceive);
}
while (bytesReceive > 0);

Thanx everybody for your help! 感谢大家的帮助!

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

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