HttpComponents - HttpClient 获取 Cookie 的两种方式

一、旧版本的 HttpClient 获取 Cookies 

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

public static String dooPost_deprecated(String url, Map<String, String> map, String charset) {
    DefaultHttpClient httpClient = null;
    HttpPost httpPost = null;
    String result = null;
    try {
        httpClient = new DefaultHttpClient();
        httpPost = new HttpPost(url);
        // 设置参数
        List<NameValuePair> list = new ArrayList<NameValuePair>();
        Iterator<Entry<String, String>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Entry<String, String> elem = (Entry<String, String>) iterator.next();
            list.add(new BasicNameValuePair(elem.getKey(), elem.getValue()));
        }
        if (list.size() > 0) {
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, charset);
            httpPost.setEntity(entity);
        }
        HttpResponse response = httpClient.execute(httpPost);
        System.out.println(response.getStatusLine().getStatusCode());
        String JSESSIONID = null;
        String cookie_user = null;
        //获得Cookies
        CookieStore cookieStore = httpClient.getCookieStore();
        List<Cookie> cookies = cookieStore.getCookies();
        for (int i = 0; i < cookies.size(); i++) {
            //遍历Cookies
            System.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 (Exception ex) {
        ex.printStackTrace();
    }
    return result;
}

二、新版本的 HttpClient 获取 Cookies 

使用 CloseableHttpClient 类实例化 HttpClient 对象。

public static String doPost(Map<String, String> map, String charset) {
    CloseableHttpClient httpClient = null;
    HttpPost httpPost = null;
    String result = null;
    try {
        CookieStore cookieStore = new BasicCookieStore();
        httpClient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
        httpPost = new HttpPost("http://localhost:8080/testtoolmanagement/LoginServlet");
        List<NameValuePair> list = new ArrayList<NameValuePair>();
        Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Entry<String, String> elem = (Entry<String, String>) iterator.next();
            list.add(new BasicNameValuePair(elem.getKey(), elem.getValue()));
        }
        if (list.size() > 0) {
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, charset);
            httpPost.setEntity(entity);
        }
        httpClient.execute(httpPost);
        String JSESSIONID = null;
        String cookie_user = null;
        List<Cookie> cookies = cookieStore.getCookies();
        for (int i = 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 (Exception ex) {
        ex.printStackTrace();
    }
    return result;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值