【问题标题】:How to add api key in request header using web api如何使用 web api 在请求标头中添加 api 密钥
【发布时间】:2019-05-02 05:52:29
【问题描述】:

大家好,这是我第一次使用 web api,我希望你能指出我正确的方向。如何使用 web api 在请求头中添加 api 键?

我试图检查谷歌,但我不确定我是否正在查看正确的指南。 这是我发现的>How to add and get Header values in WebApi

我的目标是发出 GET 请求并在请求标头中添加 API 密钥。

【问题讨论】:

  • 您是否必须使用 Get API 在标头中传递授权令牌?
  • 是的,您可以使用链接问题的方法...没有错!

标签: c# asp.net-web-api2


【解决方案1】:

您始终在任何 API 请求的标头中都有键值对。例如,这里的标题键为“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;

【讨论】:

    【解决方案2】:

    如果您使用 DI,您可以通过在 Startup.cs 中进行一些设置来轻松注入已配置的 HttpClient

    以下是配置 HttpClient 以与 Microsoft 的 App Insights api 一起使用的工作示例。当然,您必须根据需要更改标题。

    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"); 
    

    【讨论】:

      【解决方案3】:

      试试这个,希望对你有用。

                  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());
                      }
                  }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-03-21
        • 2018-01-21
        • 2018-07-29
        • 2020-12-31
        • 1970-01-01
        • 2021-04-01
        • 2018-03-19
        • 2013-05-29
        相关资源
        最近更新 更多