How to fix this Timer Tick Error in C#?

Posted by Iman Hejazi on Stack Overflow See other posts from Stack Overflow or by Iman Hejazi
Published on 2011-06-28T12:50:12Z Indexed on 2011/06/28 16:22 UTC
Read the original article Hit count: 265

Filed under:

I use this code to receive data from a internet address by a Timer in defined period of times. But when the timer ticks in second time an error will raised from this line:

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

I want to know how to fix this problem. Timer Interval: 30secs

private void timer1_Tick(object sender, EventArgs e)
    {

        WebRequest request = WebRequest.Create("http://localhost/go/online.asp?prog=y&rln=" + Properties.Settings.Default.cos);
        request.Credentials = CredentialCache.DefaultCredentials;
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream dataStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(dataStream);
        string responseFromServer = reader.ReadToEnd();
        reader.Close();
        dataStream.Close();
        response.Close();
    }

© Stack Overflow or respective owner

Related posts about c#