我必须检查一些域的 http 状态代码,尽可能减少流量。
我决定使用套接字。问题是系统总是接收完整的标头,而不仅仅是 20 个字节。
我怎样才能更多地减少响应?
这是代码...工作正常
string uri = "www.stackoverflow.com";
var addresses = System.Net.Dns.GetHostAddresses(uri);
IPEndPoint hostep = new IPEndPoint(addresses[0], 80);
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sock.Connect(hostep);
if (sock.Connected)
{
byte[] msg = Encoding.UTF8.GetBytes("HEAD / HTTP/1.1\r\nHost: "+uri+"\r\nConnection: Close\r\n\r\n");
int i = sock.Send(msg,0,msg.Length);
byte[] bytes = new byte[20];
i = sock.Receive(bytes);
string header = Encoding.ASCII.GetString(bytes);
Console.WriteLine(header);
}
感谢每一个小小的帮助!