大家好,这是我第一次使用 web api,我希望你能指出我正确的方向。如何使用 web api 在请求 header 中添加 api key ?
我试图检查谷歌,但我不确定我是否在看正确的指南。
这是我发现的> How to add and get Header values in WebApi
我的目标是发出 GET 请求并在请求 header 中添加 API key 。
最佳答案
您始终在任何 API 请求的 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;
关于c# - 如何使用 web api 在请求头中添加 api key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53551361/