我试图通过TCP流发送GET请求的页面。
下面是我的代码如下所示:
public class SocketLevelWebClient
{
public string SendWebRequest(string url, string request)
{
using(TcpClient tc = new TcpClient())
{
tc.Connect(url, 80);
using (NetworkStream ns = tc.GetStream())
{
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(ns))
{
using (System.IO.StreamReader sr = new System.IO.StreamReader(ns))
{
sw.Write(request);
sw.Flush();
return sr.ReadToEnd();
}
}
}
}
}
和请求本身:
SocketLevelWebClient wc = new SocketLevelWebClient();
var r=wc.SendWebRequest("www.youtube.com",@"GET http://www.youtube.com/ HTTP/1.1
Accept: application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, */*
Accept-Language: en-US
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
Host: www.youtube.com"+"\r\n\r\n");
当我把这个代码,它总是冻结等待服务器的响应。
我究竟做错了什么?