Setting Authorization Header of HttpClient

前端 未结 21 2183
粉色の甜心
粉色の甜心 2020-11-22 14:53

I have an HttpClient that I am using for a REST API. However I am having trouble setting up the Authorization header. I need to set the header to the token I received from d

21条回答
  •  萌比男神i
    2020-11-22 15:27

    6 Years later but adding this in case it helps someone.

    https://www.codeproject.com/Tips/996401/Authenticate-WebAPIs-with-Basic-and-Windows-Authen

    var authenticationBytes = Encoding.ASCII.GetBytes(":");
    using (HttpClient confClient = new HttpClient())
    {
      confClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", 
             Convert.ToBase64String(authenticationBytes));
      confClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(Constants.MediaType));  
      HttpResponseMessage message = confClient.GetAsync("").Result;
      if (message.IsSuccessStatusCode)
      {
        var inter = message.Content.ReadAsStringAsync();
        List result = JsonConvert.DeserializeObject>(inter.Result);
      }
    }
    

提交回复
热议问题