这是我的临时代码:
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();
}
}
}
当我将方法中请求消息的 HTTP 部分更改SendRequest
为 HTTP/1.0 时 - 一切正常。但是当我尝试在 HTTP/1.1 上重复此操作时,此块do { .. } while (bytesCount > 0)
在 181 循环中挂起。看起来服务器或客户端无法处理最后一块字节。任何人都可以解释这背后的原因以及我如何通过对现有代码的最小更改来修复它。