向 HttpClient 添加身份验证 header

Adding authentication header to HttpClient

我正在尝试访问 API,但所有文档都在 PHP 中,我对 PHP 不是很熟悉。我在向 API 进行身份验证时遇到问题。 documentation is here.

这是我目前的情况

var webAddress = "https://xboxapi.com/v2/latest-xbox360-games";

var httpResponse = (new HttpClient().GetAsync(webAddress)).Result;
httpResponse.EnsureSuccessStatusCode();
var jsonResponse = httpResponse.Content.ReadAsStringAsync().Result;

我只是不确定如何添加他们在 PHP 中使用的身份验证 header。

如有任何帮助,我们将不胜感激。

要添加自定义 header(在本例中为 X-AUTH),您需要发送自定义 HttpRequestMessage。例如:

var webAddress = "https://xboxapi.com/v2/latest-xbox360-games";

HttpClient client = new HttpClient();
HttpRequestMessage msg = new HttpRequestMessage(HttpMethod.Get, webAddress);
msg.Headers.Add('X-AUTH', 'your-auth-key-here');

HttpResponseMessage response = await client.SendAsync(msg);