zoukankan      html  css  js  c++  java
  • HttpClient获取Cookie的两种方式

    转载:http://blog.csdn.net/zhangbinu/article/details/72777620

    一、旧版本的HttpClient获取Cookies 
    p.s. 该方式官方已不推荐使用 
    使用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;
        }

    HttpClient及其jar包下载地址:官网链接

  • 相关阅读:
    java代码中存在的Big Endian 和 Little Endian
    通过hbase实现日志的转存(MR AnalyserLogDataRunner和AnalyserLogDataMapper)
    hbase使用MapReduce操作1(基本增删改查)
    配置好Nginx后,通过flume收集日志到hdfs(记得生成本地log时,不要生成一个文件,)
    数据库分页查询
    将博客搬至CSDN
    Dev Winform本地化
    C#,OleDbType,Access 对应数据类型
    C#字符串转义和反转义
    C# 正则表达式
  • 原文地址:https://www.cnblogs.com/ceshi2016/p/7244608.html
Copyright © 2011-2022 走看看