HTTp请求中的TCPClient [英] TCPClient in HTTp Request

查看:61
本文介绍了HTTp请求中的TCPClient的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用tcpClient从net获取html文件,而不是使用WebClient或WebRequest获取



主要部分源代码如下:


private void tcpconnect()

{

tcp = new TcpClient(" www.yahoo.com",80);

tcp.NoDelay = false;

tcp.ReceiveTimeout = 60000;

tcp.ReceiveBufferSize = 25000;

stream = tcp.GetStream();

byte [] send = Encoding.ASCII.GetBytes(" GET /index.html HTTP / 1.0\ \ n \\\ n \\ n   

stream.Write(send,0,send.Length);


byte [] receive = new byte [tcp.ReceiveBufferSize];


int lastreceive = stream.Read(receive,0,tcp.ReceiveBuffe rSize);

string str = Encoding。 ASCII.GetString(receive,0,tcp.ReceiveBuff erSize);


textBox1.Text = str;

tcp.Close();

stream.Close();

}


但问题是,虽然我尝试运行这个函数,它没有为我读取所有的

html源代码,但只是一部分,我尝试运行两次读取

方法功能,它继续为我读。我认为那里

可能是其他一些功能,它们可以让我们检查html文件是否已经完成加载,但是我不知道是哪个是我和我找不到它通过msdn库找到
。是否有人可以提供帮助?


感谢所有

i''m trying using the tcpClient to get a html file from net, instead of
using WebClient or WebRequest,

the main part of the source code is like this:

private void tcpconnect()
{
tcp=new TcpClient("www.yahoo.com",80);
tcp.NoDelay=false;
tcp.ReceiveTimeout=60000;
tcp.ReceiveBufferSize=25000;
stream = tcp.GetStream();
byte[] send = Encoding.ASCII.GetBytes("GET /index.html HTTP/1.0\r\n\r\n");
stream.Write(send,0,send.Length);

byte[] receive = new byte[tcp.ReceiveBufferSize];

int lastreceive=stream.Read(receive,0,tcp.ReceiveBuffe rSize);
string str = Encoding.ASCII.GetString(receive,0,tcp.ReceiveBuff erSize);

textBox1.Text=str;
tcp.Close();
stream.Close();
}

but the problem is, while i try to run this function, it didn''t read all the
html source code for me, but just a part, i try to run twice of the read
method in the function, and it does continue reading for me. I think there
might be some other function which allow us to check whether the html file is
finished loading or not, but i''m dunno which is it and i can''t find it
through the msdn library. Is that anyone could help?

Thank for all

推荐答案

TcpClient只是一种通信协议。它什么都不知道

HTML,甚至什么都没有完全阅读。这取决于您的应用

来解决这个问题。


要确定您是否已阅读所有HTML,请

远程服务器将关闭TCP会话,在这种情况下你可以检查

一些标志(不记得哪一个)或者连接是否是

未关闭,暗示您已经阅读了所有数据

遇到< / HTML>标签。


最好的问候

Johann Blake

The TcpClient is only a communications protocol. It knows nothing of
HTML or even when something has been entirely read. It''s up to your app
to figure that out.

To determine whether you have read in all of the HTML, either the
remote server will close the TCP session, in which case you can check
some flag for that (can''t remember which one) or if the connection is
not closed, it is implied that you have read all of the data when you
encounter an "</HTML> tag.

Best Regards
Johann Blake


你试过读表吗?另一个网站上的小网页?

另外尝试增加缓冲区。使用读取数据包的工具是有用的,例如MS Fiddler。还有其他一些。


Brett

Have you tried reading form a smaller webpage on a different site?
Also try increasing the buffer. Using a tool that reads you packets is
also helpful, such as MS Fiddler. There are others available as well.

Brett





你为什么要这样做?


你需要实施HTTP协议。


在你的情况下我认为问题是在接收端,如果你改变它就好了:


StreamReader reader = new StreamReader(stream);


字符串行;

while((line = reader.ReadLine())!= null)

Console.Write(line);


reader.Close();


欢呼,


-

Ignacio Machin,

ignacio.machin at dot.state.fl.us

佛罗里达州交通局


" jin" < ji*@discussions.microsoft.com>在留言中写道

新闻:AB ********************************** @ microsof t.com ...
Hi,

and why are you doing this?

you will need to implement the HTTP protocol.

In your case I think that the problem is in the receiving end, what if you
change it like:

StreamReader reader = new StreamReader( stream );

string line;
while ( (line=reader.ReadLine())!=null )
Console.Write( line );

reader.Close();

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"jin" <ji*@discussions.microsoft.com> wrote in message
news:AB**********************************@microsof t.com...
我正在尝试使用tcpClient从net获取html文件,而不是使用WebClient或WebRequest

源代码的主要部分是这样的:

private void tcpconnect()
{tcp = new TcpClient(" www.yahoo.com",80) ; tcp.NoDelay = false;
tcp.ReceiveTimeout = 60000;
tcp.ReceiveBufferSize = 25000;
stream = tcp.GetStream();
byte [] send = Encoding.ASCII.GetBytes(" GET /index.html HTTP / 1.0 \\\\\\\ nn;);
stream.Write(send,0,send.Length);

byte [] receive = new byte [tcp.ReceiveBufferSize];

int lastreceive = stream.Read(receive,0,tcp.ReceiveBuffe rSize);
string str = Encoding.ASCII.GetString(receive,0,tcp.ReceiveBuff erSize);

textBox1.Text = str;
tcp.Close();
stream.Close ();
}

但问题是我s,虽然我尝试运行这个功能,但它并没有为我阅读所有
html源代码,但只是一部分,我尝试运行两次读取
方法在功能,它继续为我阅读。我认为
可能还有一些其他功能可以让我们检查html文件
是否已经完成加载,但是我不知道它是什么,我不能通过msdn库找到它。是否有人可以提供帮助?

感谢所有人
i''m trying using the tcpClient to get a html file from net, instead of
using WebClient or WebRequest,

the main part of the source code is like this:

private void tcpconnect()
{
tcp=new TcpClient("www.yahoo.com",80);
tcp.NoDelay=false;
tcp.ReceiveTimeout=60000;
tcp.ReceiveBufferSize=25000;
stream = tcp.GetStream();
byte[] send = Encoding.ASCII.GetBytes("GET /index.html HTTP/1.0\r\n\r\n");
stream.Write(send,0,send.Length);

byte[] receive = new byte[tcp.ReceiveBufferSize];

int lastreceive=stream.Read(receive,0,tcp.ReceiveBuffe rSize);
string str = Encoding.ASCII.GetString(receive,0,tcp.ReceiveBuff erSize);

textBox1.Text=str;
tcp.Close();
stream.Close();
}

but the problem is, while i try to run this function, it didn''t read all
the
html source code for me, but just a part, i try to run twice of the read
method in the function, and it does continue reading for me. I think there
might be some other function which allow us to check whether the html file
is
finished loading or not, but i''m dunno which is it and i can''t find it
through the msdn library. Is that anyone could help?

Thank for all



这篇关于HTTp请求中的TCPClient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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