如何使用网络 api 在请求 header 中添加 api 键
How to add api key in request header using web api
大家好,这是我第一次使用网络 api,希望您能为我指明正确的方向。如何使用网络 api 在请求 header 中添加 api 键?
我试图检查 google 但我不确定我是否在看正确的指南。
这是我发现的 > How to add and get Header values in WebApi
我的目标是发出 GET 请求并在请求 header 中添加 API 键。
试试这个,我希望这对你有用。
using (var httpClient = new HttpClient())
{
httpClient.BaseAddress = new Uri("API URL");
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Authorization = new
System.Net.Http.Headers.AuthenticationHeaderValue("Pass your token value or API key");
HttpResponseMessage response = await httpClient.GetAsync(endpoint);
if (response.StatusCode == HttpStatusCode.OK)
{
string result = await response.Content.ReadAsStringAsync();
if (string.IsNullOrEmpty(result))
return "Success";
else
return result;
}
else if (response.StatusCode == HttpStatusCode.Unauthorized)
{
throw new UnauthorizedAccessException();
}
else
{
throw new Exception(await response.Content.ReadAsStringAsync());
}
}
在任何 API 请求的 header 中,您总是有 key-value 对。例如这里有 header,键为“api_key”,值为“1234”。您可以通过下面给出的方式将其添加到您的 Http 请求中。
HttpClient httpClient = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage();
request.RequestUri = new Uri("Your_get_URI");
request.Method = HttpMethod.Get;
request.Headers.Add("api_key", "1234");
HttpResponseMessage response = await httpClient.SendAsync(request);
var responseString = await response.Content.ReadAsStringAsync();
var statusCode = response.StatusCode;
如果您正在使用 DI,您可以通过在 Startup.cs
中进行一些设置来轻松地注入已配置的 HttpClient
以下是配置 HttpClient 以与 Microsoft 的 App Insights api 一起使用的工作示例。当然,您必须根据需要更改 header。
public void ConfigureServices(IServiceCollection services)
{
//Somewhere in the ConfigureSerices method.
services.AddHttpClient("APPINSIGHTS_CLIENT", c =>
{
c.BaseAddress = "<API_URL_HERE>";
c.DefaultRequestHeaders.Add("x-api-key", clientKey));
}
}
现在,如果您注入 IHttpClientFactory 供下游使用,并调用它,它将被配置并准备好使用,无需任何麻烦。
HttpClient client = factory.CreateClient("APPINSIGHTS_CLIENT");
大家好,这是我第一次使用网络 api,希望您能为我指明正确的方向。如何使用网络 api 在请求 header 中添加 api 键?
我试图检查 google 但我不确定我是否在看正确的指南。 这是我发现的 > How to add and get Header values in WebApi
我的目标是发出 GET 请求并在请求 header 中添加 API 键。
试试这个,我希望这对你有用。
using (var httpClient = new HttpClient())
{
httpClient.BaseAddress = new Uri("API URL");
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Authorization = new
System.Net.Http.Headers.AuthenticationHeaderValue("Pass your token value or API key");
HttpResponseMessage response = await httpClient.GetAsync(endpoint);
if (response.StatusCode == HttpStatusCode.OK)
{
string result = await response.Content.ReadAsStringAsync();
if (string.IsNullOrEmpty(result))
return "Success";
else
return result;
}
else if (response.StatusCode == HttpStatusCode.Unauthorized)
{
throw new UnauthorizedAccessException();
}
else
{
throw new Exception(await response.Content.ReadAsStringAsync());
}
}
在任何 API 请求的 header 中,您总是有 key-value 对。例如这里有 header,键为“api_key”,值为“1234”。您可以通过下面给出的方式将其添加到您的 Http 请求中。
HttpClient httpClient = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage();
request.RequestUri = new Uri("Your_get_URI");
request.Method = HttpMethod.Get;
request.Headers.Add("api_key", "1234");
HttpResponseMessage response = await httpClient.SendAsync(request);
var responseString = await response.Content.ReadAsStringAsync();
var statusCode = response.StatusCode;
如果您正在使用 DI,您可以通过在 Startup.cs
中进行一些设置来轻松地注入已配置的 HttpClient以下是配置 HttpClient 以与 Microsoft 的 App Insights api 一起使用的工作示例。当然,您必须根据需要更改 header。
public void ConfigureServices(IServiceCollection services)
{
//Somewhere in the ConfigureSerices method.
services.AddHttpClient("APPINSIGHTS_CLIENT", c =>
{
c.BaseAddress = "<API_URL_HERE>";
c.DefaultRequestHeaders.Add("x-api-key", clientKey));
}
}
现在,如果您注入 IHttpClientFactory 供下游使用,并调用它,它将被配置并准备好使用,无需任何麻烦。
HttpClient client = factory.CreateClient("APPINSIGHTS_CLIENT");