也可以直接在Headers中新增一个Key: Authorization, Value: Basic xxxx:
如下图所示:
同理:如果是JWT Token验证,写法为:
request.AddHeader("Authorization", "Bearer " + token.AccessToken);
Basic 后面为user:password形式的Base64加密字符串,中间有一个空格:
Base64加密:
var plainTextBytes = Encoding.UTF8.GetBytes("admin:1234");
string baseResult= System.Convert.ToBase64String(plainTextBytes);
Base64解码:
var base64EncodedBytes = System.Convert.FromBase64String("YWRtaW46MTIzNA==");
string test = System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
C# .net core后台请求接口:
string url = "http://127.0.0.1:8080/api/v5/clients/mqttx_c47a6ca6";
using HttpRequestMessage reqMsg = new HttpRequestMessage(HttpMethod.Get,url);
reqMsg.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
reqMsg.Headers.Authorization = new AuthenticationHeaderValue("Basic", "YWRtaW46MTIzNA==");
HttpResponseMessage responseMessage = await httpClient.SendAsync(reqMsg);
if (responseMessage.IsSuccessStatusCode)
{
string json =await responseMessage.Content.ReadAsStringAsync();
JObject obj = JObject.Parse(json);
JToken token = obj.SelectToken("$.data");
}
版权归原作者 彼岸大洋 所有, 如有侵权,请联系我们删除。