问题
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();
}
}
}
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.
回答1:
That's the keep-alive "feature" of http. The server does not disconnect after sending its message, keeping you waiting for further data.
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.
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 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
.
See http://en.wikipedia.org/wiki/Chunked_transfer_encoding
来源:https://stackoverflow.com/questions/18812927/http-1-1-request-message-cannot-get-last-chunk-of-bytes-while-getting-result-th