WebClient.DownloadString is 12 times faster than TcpClient?

六月ゝ 毕业季﹏ 提交于 2019-12-14 04:22:37

问题


It is now 8 hours and I am still trying to figure out. Code examples will be in VB.NET but I think they are also readable for C# users. Oh, and I accept C# solutions too.

This takes maximum 50ms:

Dim Data as String = wc.DownloadString("x.com")

While this takes 600ms average:

Dim client As New Net.Sockets.TcpClient
client.NoDelay = True
client.Client.NoDelay = True
client.Connect(IP, 80)
Dim ns As Net.Sockets.NetworkStream = client.GetStream
Dim gbytes(70000) As Byte
ns.Write(rbytes, 0, rbytes.Length)
ns.Flush()
ns.Read(gbytes, 0, gbytes.Length)
Dim Data as String = System.Text.Encoding.ASCII.GetString(gbytes)

Yes, I have tried stream reader, diffrent buffer sizes including less and more than the length of the response(which is about 100,000 bytes). I am using exactly same headers which is used by WebClient and tried diffrent headers too. I have also tried every combination of NoDelay.

The thing is, I don't want to use WebClient and NetworkStream.Flush is commented to be useless in .NET so is there a way out?

来源:https://stackoverflow.com/questions/24338763/webclient-downloadstring-is-12-times-faster-than-tcpclient

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!