使用httpClient4.1.2 获取url 获取cookie

 1、使用httpClient4.1.2

/**
	 * post请求  获取cookie
	 * @param url
	 * @param params
	 * @param cookie
	 * @param tk
	 * @return
	 */
	public static Map<String,Object> httpPostGetCookie(String url,Map<String,String> params,String cookie,String tk){
		Map<String,Object> map = new HashMap<>();
		DefaultHttpClient httpClient = new DefaultHttpClient();
		httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 60000); // 连接超时时间
		httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 90000);  // 数据传输时间
		HttpPost post = null;
		post = new HttpPost(url);
		post.setHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
		post.setHeader("Cookie",cookie);
        post.setHeader("tk",tk);
        // 参数
        List<NameValuePair> list = new ArrayList<NameValuePair>();
        Set<String> keySet = params.keySet();
        for(String key:keySet){
        	list.add(new BasicNameValuePair(key, params.get(key)));
        }
        try {
			post.setEntity(new UrlEncodedFormEntity(list,HTTP.UTF_8));
			HttpResponse response = httpClient.execute(post);
			String result = reponse2String(response);
			CookieStore cookieStore = httpClient.getCookieStore();
	        List<Cookie> cookies = cookieStore.getCookies();
	        String WEBTOKEN = null;
            String tk2 = null;
            String k = null;
            for(int i = 0; i < cookies.size(); i++){
 			   if (cookies.get(i).getName().equals("WEBTOKEN")) {
 				  WEBTOKEN = cookies.get(i).getValue();
                }
                if (cookies.get(i).getName().equals("tk")) {
                	   tk2 = cookies.get(i).getValue();
                }
                if (cookies.get(i).getName().equals("k")) {
                	   k = cookies.get(i).getValue();
                }
 		   }
           String cookiess = "WEBTOKEN="+WEBTOKEN+"; tk="+tk2+"; k="+k;
           map.put("cookie", cookiess);
           map.put("tk", tk2);
           map.put("result", result);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
        // 关闭
        httpClient.getConnectionManager().shutdown();
        return map;
	}
	
	/**
	 * 转化返回参数
	 * @param response
	 * @return
	 */
	public static String reponse2String(HttpResponse response){
		HttpEntity entity = response.getEntity();
		String body = null;
		try {
			body = EntityUtils.toString(entity);
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return body;
	}

通过     CookieStore cookieStore = httpClient.getCookieStore();
            List<Cookie> cookies = cookieStore.getCookies();

来获取

2、使用java 自带的url来获取  需要使用CookieManager

/**
	 * url转为二进制
	 * @param str
	 * @return
	 */
	public static Map<String,Object> url2Byte(String str){
        BufferedInputStream buffer = null;
        DataInputStream dataIn = null;
        ByteArrayOutputStream bos = null;
        DataOutputStream dos = null;
        byte[] bArray = null;
        Map<String,Object> map = new HashMap<>();
	    try {
	       CookieManager manager = new CookieManager();
		   manager.setCookiePolicy(CookiePolicy.ACCEPT_ORIGINAL_SERVER);
	       CookieHandler.setDefault(manager);
		   URL url = new URL(str);
		   URLConnection con = url.openConnection();
		   InputStream is = con.getInputStream();
		   buffer = new BufferedInputStream(is);
           dataIn = new DataInputStream(buffer);
           bos = new ByteArrayOutputStream();
           dos = new DataOutputStream(bos);
           byte[] buf = new byte[1024];
           while (true) {
               int len = dataIn.read(buf);
               if (len < 0)
                   break;
               dos.write(buf, 0, len);
           }
           List<HttpCookie> listCookie = manager.getCookieStore().getCookies();
           String JSESSIONID = null;
           String tk = null;
           String k = null;
		   for(int i = 0; i < listCookie.size(); i++){
			   if (listCookie.get(i).getName().equals("JSESSIONID")) {
                   JSESSIONID = listCookie.get(i).getValue();
               }
               if (listCookie.get(i).getName().equals("tk")) {
               	   tk = listCookie.get(i).getValue();
               }
               if (listCookie.get(i).getName().equals("k")) {
               	   k = listCookie.get(i).getValue();
               }
		   }
		   String cookie = "JSESSIONID="+JSESSIONID+"; tk="+tk+"; k="+k;
		   map.put("cookie", cookie);
		   map.put("JSESSIONID", JSESSIONID);
           bArray = bos.toByteArray();
           map.put("bArray", bArray);
		 } catch (MalformedURLException e) {
			e.printStackTrace();
		 } catch (FileNotFoundException e) {
			e.printStackTrace();
		 } catch (IOException e) {
			e.printStackTrace();
		 }
		 return map;
	} 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值