HttpClient获取Cookie的两种方式
来源:互联网 发布:saber软件百度云 编辑:程序博客网 时间:2024/05/18 01:45
一、旧版本的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包下载地址:官网链接
阅读全文
2 1
- HttpClient获取Cookie的两种方式
- 浏览器获取cookie的两种方式
- java获取页面cookie的两种方式
- Extjs cookie操作的两种方式
- android 联网请求的两种方式HttpURLConnection和HttpClient
- HTTP访问的两种方式(HttpClient和HttpURLConnection)
- HTTP访问的两种方式(HttpClient和HttpURLConnection)
- HTTP访问的两种方式(HttpClient和HttpURLConnection)
- HTTP访问的两种方式(HttpClient和HttpURLConnection)
- HTTP访问的两种方式(HttpClient和HttpURLConnection)
- HttpURLConnection和HttpClient两种连接方式的区别
- HttpClient 和 HttpURLConnection两种请求方式的写法
- Http访问的两种方式:HttpClient和HttpURLConnection
- 获取cookie的3种方式
- 获取网上数据的两种方法HttpURLConnection和HttpClient
- php httpclient 获取cookie
- HttpUrlConnection和HttpClient两种方式获取GET和POST请求
- httpclient 两种方式请求网络
- (个人)AR电子书系统创新实训第三周(2)
- cigarettes
- log4j配置
- Secondary NameNode:它究竟有什么作用?(转自:http://blog.csdn.net/xh16319/article/details/31375197)
- Git 上传文件、文件夹、项目到 Github
- HttpClient获取Cookie的两种方式
- 淘淘商城系列——dubbo监控中心
- Java中this关键字的详解
- ASP.NET 母版页学习记录
- JDBC连接MySQL
- 哈希表学习笔记1
- 桌面开发。1
- Java中的join和yield辨识
- opengl 画骨架