我正在使用下面的代码通过 WebRequest 从我的 C# 代码进行 API 调用:
public object GetData()
{
object response = "";
string token = "EF232354";
string baseUrl = ConfigurationManager.AppSettings["BaseURL"].ToString();
string endPoint = ConfigurationManager.AppSettings["EndPoint"].ToString();
var httpWebRequest = (HttpWebRequest) WebRequest.Create(baseUrl + endPoint);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = HttpVerb.GET.ToString();
httpWebRequest.Headers.Add("token", token);
var httpResponse = (HttpWebResponse) httpWebRequest.GetResponse();
Stream dataStream = httpResponse.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
using(JsonReader sdr = new JsonTextReader(reader))
{
JsonSerializer serializer = new JsonSerializer();
response = serializer.Deserialize(sdr);
}
return response;
}
有时我会得到:
消息:远程服务器返回错误:(504)网关超时。
异常类型:System.Net.WebException
WebRequest 一次可以发出多少个请求?
BIG阳
相关分类