HttpComponents - HttpClient 获取 Cookie 的两种方式

简介: HttpComponents - HttpClient 获取 Cookie 的两种方式

一、旧版本的 HttpClient 获取 Cookies

Ps:该方式官方已不推荐使用;使用 DefaultHttpClient 类实例化 HttpClient 对象。

publicstaticStringdooPost_deprecated(Stringurl, Map<String, String>map, Stringcharset) {
DefaultHttpClienthttpClient=null;
HttpPosthttpPost=null;
Stringresult=null;
try {
httpClient=newDefaultHttpClient();
httpPost=newHttpPost(url);
// 设置参数List<NameValuePair>list=newArrayList<NameValuePair>();
Iterator<Entry<String, String>>iterator=map.entrySet().iterator();
while (iterator.hasNext()) {
Entry<String, String>elem= (Entry<String, String>) iterator.next();
list.add(newBasicNameValuePair(elem.getKey(), elem.getValue()));
        }
if (list.size() >0) {
UrlEncodedFormEntityentity=newUrlEncodedFormEntity(list, charset);
httpPost.setEntity(entity);
        }
HttpResponseresponse=httpClient.execute(httpPost);
System.out.println(response.getStatusLine().getStatusCode());
StringJSESSIONID=null;
Stringcookie_user=null;
//获得CookiesCookieStorecookieStore=httpClient.getCookieStore();
List<Cookie>cookies=cookieStore.getCookies();
for (inti=0; i<cookies.size(); i++) {
//遍历CookiesSystem.out.println(cookies.get(i));
System.out.println("cookiename=="+cookies.get(i).getName());
System.out.println("cookieValue=="+cookies.get(i).getValue());
System.out.println("Domain=="+cookies.get(i).getDomain());
System.out.println("Path=="+cookies.get(i).getPath());
System.out.println("Version=="+cookies.get(i).getVersion());
if (cookies.get(i).getName().equals("JSESSIONID")) {
JSESSIONID=cookies.get(i).getValue();
            }
if (cookies.get(i).getName().equals("cookie_user")) {
cookie_user=cookies.get(i).getValue();
            }
        }
if (cookie_user!=null) {
result=JSESSIONID;
        }
    } catch (Exceptionex) {
ex.printStackTrace();
    }
returnresult;
}

二、新版本的 HttpClient 获取 Cookies

使用 CloseableHttpClient 类实例化 HttpClient 对象。

publicstaticStringdoPost(Map<String, String>map, Stringcharset) {
CloseableHttpClienthttpClient=null;
HttpPosthttpPost=null;
Stringresult=null;
try {
CookieStorecookieStore=newBasicCookieStore();
httpClient=HttpClients.custom().setDefaultCookieStore(cookieStore).build();
httpPost=newHttpPost("http://localhost:8080/testtoolmanagement/LoginServlet");
List<NameValuePair>list=newArrayList<NameValuePair>();
Iterator<Map.Entry<String, String>>iterator=map.entrySet().iterator();
while (iterator.hasNext()) {
Entry<String, String>elem= (Entry<String, String>) iterator.next();
list.add(newBasicNameValuePair(elem.getKey(), elem.getValue()));
        }
if (list.size() >0) {
UrlEncodedFormEntityentity=newUrlEncodedFormEntity(list, charset);
httpPost.setEntity(entity);
        }
httpClient.execute(httpPost);
StringJSESSIONID=null;
Stringcookie_user=null;
List<Cookie>cookies=cookieStore.getCookies();
for (inti=0; i<cookies.size(); i++) {
if (cookies.get(i).getName().equals("JSESSIONID")) {
JSESSIONID=cookies.get(i).getValue();
            }
if (cookies.get(i).getName().equals("cookie_user")) {
cookie_user=cookies.get(i).getValue();
            }
        }
if (cookie_user!=null) {
result=JSESSIONID;
        }
    } catch (Exceptionex) {
ex.printStackTrace();
    }
returnresult;
}
目录
相关文章
|
数据安全/隐私保护
轻松把玩HttpClient之封装HttpClient工具类(五),携带Cookie的请求
       最近更新了一下HttpClientUtil工具类代码,主要是添加了一个参数HttpContext,这个是用来干嘛的呢?其实是用来保存和传递Cookie所需要的。
1288 0
|
2月前
|
存储 自然语言处理 API
Session、cookie、token有什么区别?
Session、cookie、token有什么区别?
24 1
|
3月前
|
存储 开发框架 NoSQL
ASP.NET WEB——项目中Cookie与Session的用法
ASP.NET WEB——项目中Cookie与Session的用法
37 0
|
3月前
|
存储 安全 API
Cookie,Session和Token
Cookie,Session和Token
|
4天前
|
存储 安全 前端开发
禁用Cookie后Session还能用吗?
禁用Cookie后Session还能用吗?
14 1
|
4天前
|
Java
Cookie和Session
Cookie和Session
12 0
|
16天前
|
存储 JSON 安全
|
18天前
|
存储 前端开发 数据安全/隐私保护
网站开发--Cookie 和 Session 的工作流程
网站开发--Cookie 和 Session 的工作流程
17 0
|
22天前
|
存储
cookie与Session
cookie与Session
|
28天前
|
存储 安全 Java
理解Session和Cookie:Java Web开发中的用户状态管理
【4月更文挑战第3天】本文探讨了Web应用中用户状态管理的两种主要机制——Session和Cookie。Session在服务器端存储数据,更安全,适合大量数据,而Cookie存储在客户端,可能影响性能但支持持久化。在Java Web开发中,使用Servlet API操作Session和Cookie,注意敏感信息安全、Session管理及Cookie安全设置。理解两者差异并恰当使用是优化应用性能和用户体验的关键。