2 回答

TA貢獻(xiàn)1998條經(jīng)驗(yàn) 獲得超6個贊
使用并發(fā)哈希映射將是實(shí)現(xiàn)您想要做的最簡單的方法。
此外,如果您使用 Spring,您可能需要創(chuàng)建一個 bean 來保存 HTTP 客戶端。

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超6個贊
你為什么要做這一切?CookieStore可以使用 local 為每個請求分配不同的HttpContext.
如果需要,可以維護(hù)CookieStore每個唯一用戶的實(shí)例映射。
CloseableHttpClient httpclient = HttpClients.createDefault();
CookieStore cookieStore = new BasicCookieStore();
// Create local HTTP context
HttpClientContext localContext = HttpClientContext.create();
// Bind custom cookie store to the local context
localContext.setCookieStore(cookieStore);
HttpGet httpget = new HttpGet("http://httpbin.org/cookies");
System.out.println("Executing request " + httpget.getRequestLine());
// Pass local context as a parameter
CloseableHttpResponse response = httpclient.execute(httpget, localContext);
try {
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
List<Cookie> cookies = cookieStore.getCookies();
for (int i = 0; i < cookies.size(); i++) {
System.out.println("Local cookie: " + cookies.get(i));
}
EntityUtils.consume(response.getEntity());
} finally {
response.close();
}
添加回答
舉報(bào)