简体   繁体   中英

Adding authentication header to HttpClient

I'm trying to access an API, but all the documentation is in PHP and I'm not very familiar with PHP. I am having trouble authenticating to the API. The documentation is here .

Here is what I have so far

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

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

I'm just not sure how to add the authentication header that they are using in PHP.

Any help would be appreciated.

To add a custom header (in this case X-AUTH ), you need to send a custom HttpRequestMessage . For example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM