Setting Authorization Header of HttpClient

前端 未结 21 2284
粉色の甜心
粉色の甜心 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条回答
  •  星月不相逢
    2020-11-22 15:18

    In the case you want to send HttpClient request with Bearer Token, this code can be a good solution:

    var requestMessage = new HttpRequestMessage
    {
        Method = HttpMethod.Post,
        Content = new StringContent(".....", Encoding.UTF8, "application/json"),
        RequestUri = new Uri(".....")
    };
    
    requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "Your token");
    
    var response = await _httpClient.SendAsync(requestMessage);
    

提交回复
热议问题