码迷,mamicode.com
首页 > Web开发 > 详细

使用httpClient获取请求cookie

时间:2018-02-07 14:49:55      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:close   out   post   for   pre   print   stat   login   exce   

package mytest;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

public class Test3 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		 CloseableHttpClient httpClient = null;
		 //创建GET请求
		 HttpGet httpget = new HttpGet("对应请求的URL");
	        String result = null;
	        try {
	            CookieStore cookieStore = new BasicCookieStore();
	            httpClient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
	            httpClient.execute(httpget);
	            String JSESSIONID = 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();
	                    System.out.println(JSESSIONID);
	                }
	            }
	        } catch (Exception ex) {
	            ex.printStackTrace();
	        }

	}

}
参考:http://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples/client/ClientFormLogin.java

  

使用httpClient获取请求cookie

标签:close   out   post   for   pre   print   stat   login   exce   

原文地址:https://www.cnblogs.com/576575478-qq/p/8425681.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!