用TcpClient如何获取远程网页的内容

用TcpClient如何获取远程网页的内容

 

用TcpClient如何获取远程网页的内容
private string GetHTMLTCP(string URL)
    {
        string strHTML = "";//用来保存获得的HTML代码
        TcpClient clientSocket = new TcpClient();
        Uri URI = new Uri(URL);
        clientSocket.Connect(URI.Host, URI.Port);
        StringBuilder RequestHeaders = new StringBuilder();//用来保存HTML协议头部信息
        RequestHeaders.AppendFormat("{0} {1} HTTP/1.1\r\n", "GET"/*此处可填写GET或POST*/, URI.PathAndQuery);
        RequestHeaders.AppendFormat("Connection:close\r\n");
        RequestHeaders.AppendFormat("Host:{0}\r\n", URI.Host);
        RequestHeaders.AppendFormat("Accept:*/*\r\n");
        RequestHeaders.AppendFormat("Accept-Language:zh-cn\r\n");
        RequestHeaders.AppendFormat("User-Agent:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)\r\n\r\n");
 
 
        byte[] request = Encoding.Default.GetBytes(RequestHeaders.ToString());
        clientSocket.Client.Send(request);
        //获取要保存的网络流
        Stream readStream = clientSocket.GetStream();
        StreamReader sr = new StreamReader(readStream, Encoding.Default);
        strHTML = sr.ReadToEnd();
 
        readStream.Close();
        clientSocket.Close();
 
        return strHTML;
    }
用TcpClient如何获取远程网页的内容

用TcpClient如何获取远程网页的内容,布布扣,bubuko.com

用TcpClient如何获取远程网页的内容

上一篇:Cleaner 类|学习笔记


下一篇:MVC4 EF linq从客户端中检测到有潜在的危险的Request.Path值