如何通过TcpClient获取页面? [英] How to get page via TcpClient?

查看:215
本文介绍了如何通过TcpClient获取页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过TCP流向页面发送GET请求。

I'm trying to send a GET request to a page via TCP stream.

以下是我的代码:

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");

当我调用此代码时,它总是会冻结等待来自服务器的响应。

When I call this code, it always freezes waiting for the response from the server.

我做错了什么?

推荐答案

问题在于 ReadToEnd 仅在流结束时返回。不幸的是,服务器使TCP连接保持活动状态。因此 ReadToEnd 永远无法检测到真正的结束已到达。

The problem is that ReadToEnd only returns when the stream has ended. Unfortunately, the server keeps the TCP connection alive. Therefore ReadToEnd can never detect that the true end has arrived.

证明:

                        sw.Write(request);
                        sw.Flush();
                        var l = sr.ReadLine();

l 正在填充第一行请求。

l is being filled with the first line of the request.

删除 keep-alive 标题并添加:

Connection: close

或者使用响应 Content-Length 标题以正确读取它(二进制)。

Or use the response Content-Length header to correctly read it (binary).

这篇关于如何通过TcpClient获取页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆