出于某种原因,当我尝试从我正在使用的 Web API 获取 JSON 结果时,我在 Visual Studio 中收到403: Forbidden异常。当我尝试在我的 Web 浏览器中使用 Web API URL 时,一切都按原样显示。有任何想法吗?
主要代码:
theURI = "http://isitup.org/duckduckgo.com.json";
HttpClient httpClient = new HttpClient();
httpClient.MaxResponseContentBufferSize = Int32.MaxValue;
var jsonResponse = await httpClient.GetStringAsync(theURI);
var rootObject = JsonConvert.DeserializeObject<StatusMessage.RootObject>(jsonResponse);
int statuscode = rootObject.status_code;
if (statuscode == 1)
{
txtStatus.Text = "Website is UP!";
}
else if (statuscode == 2)
{
txtStatus.Text = "Website is DOWN!";
}
else
{
txtStatus.Text = "Invalid domain!";
}
和类文件
class StatusMessage
{
public class RootObject
{
public string domain { get; set; }
public int port { get; set; }
public int status_code { get; set; }
public object response_ip { get; set; }
public object response_code { get; set; }
public object response_time { get; set; }
}
}