1

I have several Http Web Requests in various loops etc. The web requests get data from a variety of APIs.

These seem to work some of the time, but most of the time (recently) I am getting Timeout exception errors (Operation has timed out) and am not sure why.

I accept that every so often or once in a while you will get a time out error, but this is happening too often.

Here are two of my WebRequest codes:

    public static EventList getEvents()
    {
        Uri myURI = new Uri("http://feeds.betway.com/events?key=XXX&keywords=horse-racing,uk-and-ireland&and=true");

        WebRequest webRequest = WebRequest.Create(myURI);
        webRequest.Timeout = 3000;

        using (WebResponse webResponse = webRequest.GetResponse())
        {
            using (Stream stream = webResponse.GetResponseStream())
            {
                using (var reader = XmlReader.Create(stream))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(EventList));
                    EventList data = (EventList)serializer.Deserialize(reader);

                    return data;
                }
            }
        }
    }

  public static List<WilliamHillData.Event> GetAllCompetitionEvents(string compid)
  {
        string res = "";
        Uri myURI = new Uri("https://gw.whapi.com/v2/sportsdata/competitions/" + compid + "/events/?&sort=startDateTime");

        WebRequest webRequest = WebRequest.Create(myURI);
        webRequest.Headers.Add("Content-Type", "application/json");
        webRequest.Headers.Add("apiKey", "xxx");
        webRequest.Timeout = 2000;

        using (WebResponse webResponse = webRequest.GetResponse())
        {
            using (Stream stream = webResponse.GetResponseStream())
            {
                StreamReader reader = new StreamReader(stream, Encoding.UTF8);
                res = reader.ReadToEnd();
            }
        }

        JObject jobject = JObject.Parse(res);
        List<WilliamHillData.Event> list = jobject["events"].ToObject<List<WilliamHillData.Event>>();

        return list;

  }

I cannot see anything wrong with my code as I am disposing objects correctly and have set the Timeout. Does the timeout need increasing or am I missing something?

Could this also possibly be a network issue on our end?

thehennyy
  • 4,020
  • 1
  • 22
  • 31
Zack Antony Bucci
  • 571
  • 1
  • 12
  • 29

0 Answers0