使用CookieContainer
自动管理你的HttpClient
Cookie
。
因为业务需要,客户端需要进行登录,登录的会话token
保留在cookie
呢,所以需要让HttpClient
携带上Cookie
,本文作为一个记录。
class HttpClientTool {
/**
* 如果全局cookie,可以使用静态变量,如果不是全局,可只作为内部变量
*/
private static CookieContainer cookies = new CookieContainer();
public static void main(String[] args) {
HttpClientHandler handler = new HttpClientHandler();
// 开启cookie
handler.UseCookies = true;
// 使用 CookieContainer 管理
handler.CookieContainer = cookies;
// 将 HttpClientHandler 传入HttpClient,之后发送接收请求都会通过`cookies`进行cookie管理
HttpClient httpClient = new HttpClient(handler);
}
}
MacOs
版本的 Rider
居然不支持 WinForm
,还得开个虚拟机测试。啧