HTTP 1.1请求消息不能获得最后一个字节块,而通过套接字获取结果 [英] HTTP 1.1 request message cannot get last chunk of bytes, while getting result through socket
问题描述
这是我的Scratch代码:
Here is my scratch code:
using System;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace SocketsDemo
{
class Program
{
static void Main(string[] args)
{
Uri uri;
if (args.Any()) uri = new Uri(args[0]);
else uri = new Uri("http://odetocode.com/Articles/473.aspx"); //http://www.odetocode.com
var result = GetResource(uri);
Console.WriteLine(result);
Console.ReadLine();
}
private static string GetResource(Uri uri)
{
var host = uri.Host;
var resource = uri.PathAndQuery;
var hostEntry = Dns.GetHostEntry(host);
var socket = CreateSocket(hostEntry);
SendRequest(socket, host, resource);
return GetResponse(socket);
}
private static Socket CreateSocket(IPHostEntry hostEntry)
{
const int HTTP_PORT = 80;
var endPoint = new IPEndPoint(hostEntry.AddressList[0], HTTP_PORT);
var socket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(endPoint);
return socket.Connected ? socket : null;
}
private static void SendRequest(Socket socket, string host, string resource)
{
var requestMessage = String.Format(
"GET {0} HTTP/1.1\r\n" +
"HOST: {1}\r\n" +
"\r\n", resource, host);
var requestBytes = Encoding.ASCII.GetBytes(requestMessage);
socket.Send(requestBytes);
}
private static string GetResponse(Socket socket)
{
int bytesCount = 0;
var buffer = new byte[256];
var result = new StringBuilder();
do
{
bytesCount = socket.Receive(buffer);
result.Append(Encoding.ASCII.GetString(buffer, 0, bytesCount));
} while (bytesCount > 0);
return result.ToString();
}
}
}
请求消息在 SendRequest
方法到HTTP / 1.0 - 一切工作。
但是当我尝试在HTTP / 1.1上重复这个,这个块 do {..} while(bytesCount> 0)
挂起181周期。看起来像服务器或客户端不能处理最后一块字节。
When i change HTTP part of request message in SendRequest
method to HTTP/1.0 - everything working.
But when I try to repeat this on HTTP/1.1, this block do { .. } while (bytesCount > 0)
hangs out on 181 cycle. Looks like server or client cannot deal with last chunk of bytes.
Can anybody explain what stays behind this and how i can repair this with smallest changes to existing code.
推荐答案
这是一个很好的解决方案。 http的保持活动的特征。服务器在发送其消息后不会断开连接,让您等待进一步的数据。
That's the keep-alive "feature" of http. The server does not disconnect after sending its message, keeping you waiting for further data.
您必须解析HTTP头。如果有一个 Transfer-Encoding:chunked
,你必须解析你接收的块,直到你收到一个0字节的块。
You'll have to parse the HTTP headers. If there's a Transfer-Encoding: chunked
, you'll have to parse the chunks you're receiving until you receive a chunk of 0 bytes.
如果不是以块的形式发送,你必须解析一个 Content-Length
头来查看总共读取多少字节。
If it isn't sent as chunks, you'll have to parse a Content-Length
header to see how many bytes total to read.
HTTP / 1.0不支持块,并且某些客户端不支持保持连接活动,因此对于HTTP / 1.0,保持活动 off
,而对于HTTP / 1.1,默认为 on
。
HTTP/1.0 did not support chunks and some clients did not support keeping the connection alive, so for HTTP/1.0 the default server behavior should be keep-alive off
, while for HTTP/1.1 the default is on
.
请参阅 http://en.wikipedia.org/wiki/Chunked_transfer_encoding
这篇关于HTTP 1.1请求消息不能获得最后一个字节块,而通过套接字获取结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!