I'm developing a solution in Winforms C # that receives a text file via network, via HttpWebRequest
:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(conexao + "/piece.txt");
request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10;
var sp = request.ServicePoint;
var prop = sp.GetType().GetProperty("HttpBehaviour", BindingFlags.Instance | BindingFlags.NonPublic);
prop.SetValue(sp, (byte)0, null);
And reading is done by StreamReader
:
StreamReader stream = request.GetResponse().GetResponseStream();
stream.ReadTimeout = 8600000; //Para Evitar o timeout
This file is generated by a weight scale (each weight that passes through it creates a line in the text file piece.txt
) and because of this the file has no size definition or EOF
. My problem happens when this treadmill is not passing weight and my system is trying to read the file, which is done like this:
while ((buffer = reader.ReadLine()) != null)
{....
When the system spends 1 minute without receiving text, this line of code bursts an error:
Unable to read transport connection data: An existing connection to the remote host was forced to terminate.
I've tried to use reader.Peek()
, but the same error happens.
I need a solution that does not pop this error if it does not have text, that is, it verifies that it is empty and wait until a line appears (without crashing% of the operation).
Any ideas that might help me?
A dynamic reader perhaps?