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);
}
}