0

I have a very simple program to make a web service call to a URL every minute. But I keep time out error on HttpWebResponse response = (HttpWebResponse)request.GetResponse() I am disposing properly, so what's the issue here? I suspect it's the server blocking consecutive calls, but I can keep pinging that url on my browser repeatedly and get result back.

        while (true)
        { 
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.AutomaticDecompression = DecompressionMethods.GZip;


                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { 
                    //using (Stream stream = response.GetResponseStream()) { 
                    //    using (StreamReader reader = new StreamReader(stream))
                    //    {
                    //        html = reader.ReadToEnd();
                    //    }
                    //}
                }
                var inventory = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(html);
                System.Threading.Thread.Sleep(60000);

Is there something I can change in the header that can fix this?

Edited for extra help:

              //updated base on recommendation. Now I get error 
 `System.InvalidOperationException: 'This operation cannot be performed after the request has been submitted.' on request.GetResponse()


        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.AutomaticDecompression = DecompressionMethods.GZip;

        while (true)
        {


            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                StreamReader reader = new StreamReader(response.GetResponseStream());

                html = reader.ReadToEnd();
               //do stuff with the response

                request.Abort();
                reader.Close();
                System.Threading.Thread.Sleep(600);
            }

        }
user308553
  • 1,238
  • 4
  • 18
  • 32
  • Does this answer your question? [Adjusting HttpWebRequest Connection Timeout in C#](https://stackoverflow.com/questions/1500955/adjusting-httpwebrequest-connection-timeout-in-c-sharp) – Nitin Sawant Oct 06 '20 at 16:28
  • 1
    Add a using to the request or move the request outside the while loop. You can check status of the connection from cmd.exe >Netstat -a. Disposing the response doesn't close the request. I think the issue is you are trying to send a new request connection every time through the while loop without closing previous request. – jdweng Oct 06 '20 at 16:35
  • @NitinS I dont think it's a timeout limit issue. Cause I can make the request consecutively on web browser with no problem. it's something else – user308553 Oct 06 '20 at 16:42
  • @jdweng thanks moving the request outside worked. but I also get 'Cannot access a disposed object' now on the `Stream stream = response.GetResponseStream()` line. I understand the using disposed it, but each forloop should create a new one right? – user308553 Oct 06 '20 at 16:53
  • When the reader is dispose it is also disposing the stream. So when you exit the using statement for the stream you are getting the exception. – jdweng Oct 06 '20 at 17:46
  • @jdweng I am still inside that using statement though. I even tried using dispose() instead and do all the dispose at the end of the loop. Do you have any suggestion? – user308553 Oct 06 '20 at 19:00
  • The request should be before the while loop. Then have only one using statement for the response. If you are still having an issue let me know what exception you are getting. – jdweng Oct 06 '20 at 23:06
  • @jdweng Hey I just made the change (updated in the post). However now I get error ` 'This operation cannot be performed after the request has been submitted.'` So I added request.Abort() at the end, but still getting this error. Thanks – user308553 Oct 06 '20 at 23:45
  • After you send a request you have to wait for the response before sending a second request. And you only want to use one request which is why I put the request outside the WHILE loop. The sleep time is in milliseconds so waiting 0.6 seconds may not be long enough to get response. – jdweng Oct 06 '20 at 23:53
  • @jdweng yeah I tried longer wait time, it's the same. In browser I can refresh immediately and get response. So it's probably something else. I've been stuck for hours, just cant figure what's wrong – user308553 Oct 07 '20 at 00:33
  • @jdweng oh nevermind it worked.. i brought the request back into the loop (so it create a new request everytime). I kept just using one using statement. it works now! Thanks – user308553 Oct 07 '20 at 00:36

0 Answers0