在各种循环中,我有几个Http Web请求。Web请求从各种API获取数据。
这些似乎有时会起作用,但是大多数时候(最近),我收到超时异常错误(操作已超时),并且不确定为什么。
我接受您每隔一段时间或不时地会收到超时错误,但是这种情况经常发生。
这是我的两个WebRequest代码:
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;
}
当我正确地放置对象并设置了超时时,我的代码看不到任何错误。超时需要增加还是我错过了什么?
这是否也可能是我们网络上的问题?