Dynamic input reading in C#

Asked

Viewed 370 times

1

I am 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 the reading is done by StreamReader:

 StreamReader stream = request.GetResponse().GetResponseStream();
              stream.ReadTimeout = 8600000; //Para Evitar o timeout

This file is generated by a conveyor scale (each weight that passes through it generates a line in the text file piece.txt) and due to this the file has no size definition nor EOF. My problem happens when on this mat is not passing weight and my system is trying to read the file, which is done this way:

while ((buffer = reader.ReadLine()) != null)
{....

When the System passes 1 minute without receiving text this line of code bursts an error:

Unable to read transport connection data: Forced cancellation of an existing connection by remote host.

I’ve tried using the reader.Peek(), but the same mistake happens.

I need a solution that does not explode this error if there is no text, IE, it checks that this empty and wait until a line appears (without popping timeout of the operation).

Any idea that might help me?

A dynamic reader maybe?

  • Equivalent question in the English community http://stackoverflow.com/questions/525364/how-to-download-a-file-from-a-website-in-c-sharp

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.