问题
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