年度变更建库软件5.0版本
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
1.1 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace AttachmentDemo.Helper
{
public class AuthHttpClient
{
//private static Lazy<HttpClient> InstanceLazy = new Lazy<HttpClient>(() => new HttpClient());
public static HttpClient Instance { get; set; } //=> new HttpClient();// InstanceLazy.Value;
private AuthHttpClient()
{
}
public static void SetHttpClient(string baseUrl, string token)
{
var baseAddress = new Uri(baseUrl);
// Set your cookies up in the cookie container of an HttpClientHandler
var handler = new HttpClientHandler();
handler.CookieContainer.Add(baseAddress, new Cookie("token", token));
// Use that to create a new HttpClient with the same base address
Instance = new HttpClient(handler) { BaseAddress = baseAddress };
Instance.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
}
}
}