请求 https://api.github.com/users (api) returns System.Net.HttpStatusCode.Forbidden IsSuccessStatusCode = false
Requesting https://api.github.com/users (api) returns System.Net.HttpStatusCode.Forbidden IsSuccessStatusCode = false
我试图向 https://api.github.com/users 发出 api 请求,但我收到了这个被禁止的响应。由于它不需要身份验证,我不知道为什么会返回 403。
{StatusCode: 403, Version: 1.0, Content: System.Net.Http.StreamContent,
Headers:
RequestMessage = {Method: GET, RequestUri: 'https://api.github.com/users',
Version: 1.1, Content: <null>, Headers:
{
Accept: application/json
}}
ReasonPhrase = "Forbidden"
IsSuccessStatusCode = false
以下是我的代码。
namespace ConsumingWebAapiRESTinMVC.Controllers
{
public class HomeController : Controller
{
//Hosted web API REST Service base url
string Baseurl = "https://api.github.com/users";
public async Task<ActionResult> Index()
{
List<Employee> EmpInfo = new List<Employee>();
using (var client = new HttpClient())
{
//Passing service base url
client.BaseAddress = new Uri(Baseurl);
client.DefaultRequestHeaders.Clear();
//Define request data format
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//Sending request to find web api REST service resource GetAllEmployees using HttpClient
HttpResponseMessage Res = await client.GetAsync(Baseurl);
//Checking the response is successful or not which is sent using HttpClient
if (Res.IsSuccessStatusCode)
{
//Storing the response details recieved from web api
var EmpResponse = Res.Content.ReadAsStringAsync().Result;
//Deserializing the response recieved from web api and storing into the Employee list
EmpInfo = JsonConvert.DeserializeObject<List<Employee>>(EmpResponse);
}
//returning the employee list to view
return View(EmpInfo);
}
}
}
}
如果您检查实际的响应文本 Res.Content.ReadAsStringAsync().Result
那么您会看到以下错误消息:
Request forbidden by administrative rules. Please make sure your request has a User-Agent header (http://developer.github.com/v3/#user-agent-required). Check https://developer.github.com for other possible causes.
您需要添加一个User-Agent header。
client.DefaultRequestHeaders.Add("User-Agent", "C# App");
但更普遍的是,您需要改进日志记录,以便了解无法正常工作的真正原因。
我试图向 https://api.github.com/users 发出 api 请求,但我收到了这个被禁止的响应。由于它不需要身份验证,我不知道为什么会返回 403。
{StatusCode: 403, Version: 1.0, Content: System.Net.Http.StreamContent,
Headers:
RequestMessage = {Method: GET, RequestUri: 'https://api.github.com/users',
Version: 1.1, Content: <null>, Headers:
{
Accept: application/json
}}
ReasonPhrase = "Forbidden"
IsSuccessStatusCode = false
以下是我的代码。
namespace ConsumingWebAapiRESTinMVC.Controllers
{
public class HomeController : Controller
{
//Hosted web API REST Service base url
string Baseurl = "https://api.github.com/users";
public async Task<ActionResult> Index()
{
List<Employee> EmpInfo = new List<Employee>();
using (var client = new HttpClient())
{
//Passing service base url
client.BaseAddress = new Uri(Baseurl);
client.DefaultRequestHeaders.Clear();
//Define request data format
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//Sending request to find web api REST service resource GetAllEmployees using HttpClient
HttpResponseMessage Res = await client.GetAsync(Baseurl);
//Checking the response is successful or not which is sent using HttpClient
if (Res.IsSuccessStatusCode)
{
//Storing the response details recieved from web api
var EmpResponse = Res.Content.ReadAsStringAsync().Result;
//Deserializing the response recieved from web api and storing into the Employee list
EmpInfo = JsonConvert.DeserializeObject<List<Employee>>(EmpResponse);
}
//returning the employee list to view
return View(EmpInfo);
}
}
}
}
如果您检查实际的响应文本 Res.Content.ReadAsStringAsync().Result
那么您会看到以下错误消息:
Request forbidden by administrative rules. Please make sure your request has a User-Agent header (http://developer.github.com/v3/#user-agent-required). Check https://developer.github.com for other possible causes.
您需要添加一个User-Agent header。
client.DefaultRequestHeaders.Add("User-Agent", "C# App");
但更普遍的是,您需要改进日志记录,以便了解无法正常工作的真正原因。