使用CookieContainer自动管理你的HttpClient Cookie。
因为业务需要,客户端需要进行登录,登录的会话token保留在cookie呢,所以需要让HttpClient携带上Cookie,本文作为一个记录。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| class HttpClientTool {
private static CookieContainer cookies = new CookieContainer();
public static void main(String[] args) { HttpClientHandler handler = new HttpClientHandler(); handler.UseCookies = true; handler.CookieContainer = cookies; HttpClient httpClient = new HttpClient(handler); } }
|
MacOs 版本的 Rider 居然不支持 WinForm ,还得开个虚拟机测试。啧