欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

[wpf] 07 后台身份验证和令牌代码获取 第 2 步 数据获取

最编程 2024-10-19 07:07:34
...

在执行异步获取数据时,将授权执行一次即可。
示例代码:

 public async Task<string> GetAsync<T>(string path,string bearerToken)
 {
     httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", bearerToken);

     HttpResponseMessage response = await httpClient.GetAsync("Your url");
     if (response.IsSuccessStatusCode)
     {
         string content = await response.Content.ReadAsStringAsync();
         return content;
     }

     throw new Exception($"Error getting data from API: {response.StatusCode}");
 }

推荐阅读