asp.net - C# NetworkStream.Read oddity -


Can anyone point out the faults in this code? I'm retrieving some HTML with TcpClient NetworkStream.Read () IIS does not end up while talking to the server, if I go through the Fidler proxy instead, it works fine, but when on the target server If the talk is directly talking then the exception to the .read () loop connection does not stop until a mistake like "Remote server has closed the connection".

Internal TcpClient Client {get; Set; } /// Here's another bunch of code ... try {NetworkStream ns = Client.GetStream (); StreamWriter SW = new streamer (ns); Sw.Write (request); Sw.flush (); Byte [] buffer = new byte [1024]; Int read = 0; Try {while ((read = ns.Read (buffer, 0, buffer, length)) gt; 0) {response.AppendFormat ("{0}", encoding. ASCII.GetString (buffer, 0, reading)); }} Hold // (from SocketException) {} Finally {closed}; } Update

In the debugger, I'm coming through full feedback can be immediately and attached For my stringbilder it seems that when the server is stopped sending the response or my code is not searching for it, the connection is not being closed.

Conclusion As stated here, to take advantage of protocol offerings (in case of HTTP, content-length title) to determine when this transaction is completed The Best. However, I've found that not all pages have content length set. So, now I am using a hybrid solution:

  1. For all transactions, set the request connection header to "off" So frustrated by keeping the server socket open, it improves the possibilities that the server will close the connection while responding to your request.

  2. If the content-length is set, use it to determine if a request is completed.

  3. Otherwise, Set NetworkTream's RequestTimeout property as a large, but reasonable price, such as 1 second. Then, loop on networkstream. Read () as long as there are any times), or b) read some bytes for you. > Thanks for everyone for your excellent and detailed reactions.

Not sure if this is useful or not, but with it the connection to the HTTP 1.1 server does not stop underlying Maybe, the stream may not be locked too? The idea is that you can reuse the connection to send a new request. I think you have to use content-length optionally instead of using the WebClient or WebRequest class.


Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -