简体   繁体   中英

Add X-Auth-Token - C# HttpClient

I'm trying to add an "X-Auth-Token" as a header on my HttpClient and I'm getting an 403 error forbidden when I make the request, which makes sense because I don't think my X-Auth-Token is being attached as a header.

How can I specify "X-Auth-Token" in my Header?

Here is the relevant code:

using (var c = new HttpClient())
{
    c.BaseAddress = new Uri(url); 
    c.DefaultRequestHeaders.Accept.Clear(); 
    c.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 
    c.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(token);

You can use the Add method to add the header.

c.DefaultRequestHeaders.Add("x-auth-token", token);

The constructor for AuthenticationHeaderValue accepts a scheme . I'm not certain what that is but is likely to be on of these

http://msdn.microsoft.com/en-us/library/ms789031(v=vs.110).aspx

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