• HttpClient登陆后获取并携带cookies发起请求


    最近项目中,用到了登陆后获取并携带cookies发起请求的业务场景,现总结写出来备忘一下。

    1.定义存取cookies信息的全局变量

    1 public class HttpUtil {
    2 
    3     /**
    4      * 用来存取cookies信息的变量.
    5      */
    6     private static CookieStore cookieStore;

    2.登陆后获取cookies信息存储到全局变量

     1 public class HttpUtil {
     2 
     3     /**
     4      *
     5      * @param httpUrl
     6      *            地址
     7      * @param paramMap
     8      *            参数
     9      *
    10      */
    11     public static String loginPost(String httpUrl, Map<String, Object> paramMap) throws Exception {
    12         if (log.isInfoEnabled()) {
    13             log.info("请求httpUrl=" + httpUrl);
    14             log.info("请求params=" + JSONObject.toJSONString(paramMap));
    15         }
    16 
    17         // 创建httpPost
    18         HttpPost httpPost = new HttpPost(httpUrl);
    19 
    20         // 设置参数
    21         List<NameValuePair> params = new ArrayList<>();
    22         if (paramMap != null && paramMap.size() > 0) {
    23             Set<String> keySet = paramMap.keySet();
    24             for (String key : keySet) {
    25                 params.add(new BasicNameValuePair(key, UcopCommonUtil.convertString(paramMap.get(key))));
    26             }
    27             httpPost.setEntity(new UrlEncodedFormEntity(params,"UTF-8"));
    28         }
    29 
    30         String response;
    31         DefaultHttpClient httpClient = null;
    32         try {
    33             httpClient = new DefaultHttpClient();
    34             String contentCharset = CoreProtocolPNames.HTTP_CONTENT_CHARSET;
    35             String soTimeOut = CoreConnectionPNames.SO_TIMEOUT;
    36             String connectTimeOut = CoreConnectionPNames.CONNECTION_TIMEOUT;
    37             httpClient.getParams().setParameter(contentCharset, "utf-8");
    38             httpClient.getParams().setParameter(soTimeOut, 60000);
    39             httpClient.getParams().setParameter(connectTimeOut, 60000);
    40 
    41             HttpResponse httpResponse = httpClient.execute(httpPost);
    42 
    43             // 获得返回结果
    44             response = EntityUtils.toString(httpResponse.getEntity(), UcopUtils.UCOP_DEFAULT_CHARSET);
    45 
    46             // 获得cookie并存取
    47             this.cookieStore = httpClient.getCookieStore();
    48             List<Cookie> cookieList = this.cookieStore.getCookies();
    49             for (Cookie cookie : cookieList){
    50                 String name =cookie.getName();
    51                 String value = cookie.getValue();
    52                 log.info("登陆成功,cookie name = " + name + ", cookie value = " + value);
    53             }
    54             if (log.isInfoEnabled()) {
    55                 log.info("响应内容=" + response);
    56             }
    57             return response;
    58         } catch (Exception e) {
    59             log.error("HttpPost请求失败:" + e.getMessage(), e);
    60             throw new RuntimeException("HttpPost请求失败:" + e.getMessage(), e);
    61         } finally {
    62             if (null != httpClient) {
    63                 httpClient.getConnectionManager().shutdown();
    64             }
    65         }
    66     }

    3.携带cookie发送业务请求

     1 public class HttpUtil {
     2 
     3     /**
     4      *
     5      * @param url            请求地址
     6      * @param paramMap              请求内容
     7      * @return 响应内容
     8      * @throws Exception
     9      */
    10     public static String requestPostWithCookie(String url, Map<String, Object> paramMap) throws Exception {
    11         if (log.isInfoEnabled()) {
    12             log.info("post请求地址:" + url);
    13             log.info("post请求内容:" + paramMap);
    14         }
    15 
    16         long beginTime = System.currentTimeMillis();
    17         try {
    18             HttpClient httpClient = HttpClients.custom().setDefaultCookieStore(this.cookieStore).build();
    19             HttpPost httpPost = new HttpPost(url);
    20 
    21             // 设置参数
    22             List<NameValuePair> params = new ArrayList<>();
    23             if (paramMap != null && paramMap.size() > 0) {
    24                 Set<String> keySet = paramMap.keySet();
    25                 for (String key : keySet) {
    26                     params.add(new BasicNameValuePair(key, UcopCommonUtil.convertString(paramMap.get(key))));
    27                 }
    28                 httpPost.setEntity(new UrlEncodedFormEntity(params,"UTF-8"));
    29             }
    30 
    31             // 设置请求和传输超时时间
    32             RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(120000).setConnectTimeout(120000).build();
    33             httpPost.setConfig(requestConfig);
    34 
    35             // 发起请求
    36             HttpResponse response = httpClient.execute(httpPost);
    37 
    38             // 获取响应数据并转换成JSON
    39             String resString = EntityUtils.toString(response.getEntity(),  UcopUtils.UCOP_DEFAULT_CHARSET);
    40             log.info("post响应内容: " + resString);
    41 
    42             return resString;
    43         } catch (Exception e) {
    44             log.error("请求失败:" + e.getMessage(), e);
    45             throw e;
    46         } finally {
    47             log.info("结束post请求,耗时(ms):" + (System.currentTimeMillis() - beginTime));
    48         }
    49     }    

    4.完整示例代码

      1 package com.zat.ucop.bus.service.impl;
      2 
      3 import com.alibaba.fastjson.JSONObject;
      4 import com.zat.ucop.service.util.UcopCommonUtil;
      5 import com.zat.ucop.service.util.UcopUtils;
      6 import org.apache.http.HttpResponse;
      7 import org.apache.http.NameValuePair;
      8 import org.apache.http.client.CookieStore;
      9 import org.apache.http.client.HttpClient;
     10 import org.apache.http.client.config.RequestConfig;
     11 import org.apache.http.client.entity.UrlEncodedFormEntity;
     12 import org.apache.http.client.methods.HttpPost;
     13 import org.apache.http.cookie.Cookie;
     14 import org.apache.http.impl.client.DefaultHttpClient;
     15 import org.apache.http.impl.client.HttpClients;
     16 import org.apache.http.message.BasicNameValuePair;
     17 import org.apache.http.params.CoreConnectionPNames;
     18 import org.apache.http.params.CoreProtocolPNames;
     19 import org.apache.http.util.EntityUtils;
     20 import org.slf4j.Logger;
     21 import org.slf4j.LoggerFactory;
     22 
     23 import java.util.ArrayList;
     24 import java.util.List;
     25 import java.util.Map;
     26 import java.util.Set;
     27 
     28 public class HttpUtil {
     29 
     30     private static Logger log = LoggerFactory.getLogger(HttpUtil.class);
     31 
     32     /**
     33      * 用来存取cookies信息的变量.
     34      */
     35     private static CookieStore cookieStore;
     36 
     37 
     38     /**
     39      *
     40      * @param httpUrl
     41      *            地址
     42      * @param paramMap
     43      *            参数
     44      *
     45      */
     46     public static String loginPost(String httpUrl, Map<String, Object> paramMap) throws Exception {
     47         if (log.isInfoEnabled()) {
     48             log.info("请求httpUrl=" + httpUrl);
     49             log.info("请求params=" + JSONObject.toJSONString(paramMap));
     50         }
     51 
     52         // 创建httpPost
     53         HttpPost httpPost = new HttpPost(httpUrl);
     54 
     55         // 设置参数
     56         List<NameValuePair> params = new ArrayList<>();
     57         if (paramMap != null && paramMap.size() > 0) {
     58             Set<String> keySet = paramMap.keySet();
     59             for (String key : keySet) {
     60                 params.add(new BasicNameValuePair(key, UcopCommonUtil.convertString(paramMap.get(key))));
     61             }
     62             httpPost.setEntity(new UrlEncodedFormEntity(params,"UTF-8"));
     63         }
     64 
     65         String response;
     66         DefaultHttpClient httpClient = null;
     67         try {
     68             httpClient = new DefaultHttpClient();
     69             String contentCharset = CoreProtocolPNames.HTTP_CONTENT_CHARSET;
     70             String soTimeOut = CoreConnectionPNames.SO_TIMEOUT;
     71             String connectTimeOut = CoreConnectionPNames.CONNECTION_TIMEOUT;
     72             httpClient.getParams().setParameter(contentCharset, "utf-8");
     73             httpClient.getParams().setParameter(soTimeOut, 60000);
     74             httpClient.getParams().setParameter(connectTimeOut, 60000);
     75 
     76             HttpResponse httpResponse = httpClient.execute(httpPost);
     77 
     78             // 获得返回结果
     79             response = EntityUtils.toString(httpResponse.getEntity(), UcopUtils.UCOP_DEFAULT_CHARSET);
     80 
     81             // 获得cookie并存取
     82             cookieStore = httpClient.getCookieStore();
     83             List<Cookie> cookieList = cookieStore.getCookies();
     84             for (Cookie cookie : cookieList){
     85                 String name =cookie.getName();
     86                 String value = cookie.getValue();
     87                 log.info("登陆成功,cookie name = " + name + ", cookie value = " + value);
     88             }
     89             if (log.isInfoEnabled()) {
     90                 log.info("响应内容=" + response);
     91             }
     92             return response;
     93         } catch (Exception e) {
     94             log.error("HttpPost请求失败:" + e.getMessage(), e);
     95             throw new RuntimeException("HttpPost请求失败:" + e.getMessage(), e);
     96         } finally {
     97             if (null != httpClient) {
     98                 httpClient.getConnectionManager().shutdown();
     99             }
    100         }
    101     }
    102 
    103     /**
    104      *
    105      * @param url            请求地址
    106      * @param paramMap              请求内容
    107      * @return 响应内容
    108      * @throws Exception
    109      */
    110     public static String requestPostWithCookie(String url, Map<String, Object> paramMap) throws Exception {
    111         if (log.isInfoEnabled()) {
    112             log.info("post请求地址:" + url);
    113             log.info("post请求内容:" + paramMap);
    114         }
    115 
    116         long beginTime = System.currentTimeMillis();
    117         try {
    118             HttpClient httpClient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
    119             HttpPost httpPost = new HttpPost(url);
    120 
    121             // 设置参数
    122             List<NameValuePair> params = new ArrayList<>();
    123             if (paramMap != null && paramMap.size() > 0) {
    124                 Set<String> keySet = paramMap.keySet();
    125                 for (String key : keySet) {
    126                     params.add(new BasicNameValuePair(key, UcopCommonUtil.convertString(paramMap.get(key))));
    127                 }
    128                 httpPost.setEntity(new UrlEncodedFormEntity(params,"UTF-8"));
    129             }
    130 
    131             // 设置请求和传输超时时间
    132             RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(120000).setConnectTimeout(120000).build();
    133             httpPost.setConfig(requestConfig);
    134 
    135             // 发起请求
    136             HttpResponse response = httpClient.execute(httpPost);
    137 
    138             // 获取响应数据并转换成JSON
    139             String resString = EntityUtils.toString(response.getEntity(),  UcopUtils.UCOP_DEFAULT_CHARSET);
    140             log.info("post响应内容: " + resString);
    141 
    142             return resString;
    143         } catch (Exception e) {
    144             log.error("请求失败:" + e.getMessage(), e);
    145             throw e;
    146         } finally {
    147             log.info("结束post请求,耗时(ms):" + (System.currentTimeMillis() - beginTime));
    148         }
    149     }
    150 
    151 }

    补充:HttpClient获取Cookie的两种方式

    1.使用DefaultHttpClient类实例化httpClient对象:

      1 package com.zat.ucop.bus.service.impl;
      2 
      3 import com.alibaba.fastjson.JSONObject;
      4 import com.zat.ucop.service.util.UcopCommonUtil;
      5 import com.zat.ucop.service.util.UcopUtils;
      6 import org.apache.http.HttpResponse;
      7 import org.apache.http.NameValuePair;
      8 import org.apache.http.client.CookieStore;
      9 import org.apache.http.client.HttpClient;
     10 import org.apache.http.client.config.RequestConfig;
     11 import org.apache.http.client.entity.UrlEncodedFormEntity;
     12 import org.apache.http.client.methods.HttpPost;
     13 import org.apache.http.cookie.Cookie;
     14 import org.apache.http.impl.client.DefaultHttpClient;
     15 import org.apache.http.impl.client.HttpClients;
     16 import org.apache.http.message.BasicNameValuePair;
     17 import org.apache.http.params.CoreConnectionPNames;
     18 import org.apache.http.params.CoreProtocolPNames;
     19 import org.apache.http.util.EntityUtils;
     20 import org.slf4j.Logger;
     21 import org.slf4j.LoggerFactory;
     22 
     23 import java.util.ArrayList;
     24 import java.util.List;
     25 import java.util.Map;
     26 import java.util.Set;
     27 
     28 public class HttpUtil {
     29 
     30     private static Logger log = LoggerFactory.getLogger(HttpUtil.class);
     31 
     32     /**
     33      * 用来存取cookies信息的变量.
     34      */
     35     private static CookieStore cookieStore;
     36 
     37 
     38     /**
     39      *
     40      * @param httpUrl
     41      *            地址
     42      * @param paramMap
     43      *            参数
     44      *
     45      */
     46     public static String loginPost(String httpUrl, Map<String, Object> paramMap) throws Exception {
     47         if (log.isInfoEnabled()) {
     48             log.info("请求httpUrl=" + httpUrl);
     49             log.info("请求params=" + JSONObject.toJSONString(paramMap));
     50         }
     51 
     52         // 创建httpPost
     53         HttpPost httpPost = new HttpPost(httpUrl);
     54 
     55         // 设置参数
     56         List<NameValuePair> params = new ArrayList<>();
     57         if (paramMap != null && paramMap.size() > 0) {
     58             Set<String> keySet = paramMap.keySet();
     59             for (String key : keySet) {
     60                 params.add(new BasicNameValuePair(key, UcopCommonUtil.convertString(paramMap.get(key))));
     61             }
     62             httpPost.setEntity(new UrlEncodedFormEntity(params,"UTF-8"));
     63         }
     64 
     65         String response;
     66         DefaultHttpClient httpClient = null;
     67         try {
     68             httpClient = new DefaultHttpClient();
     69             String contentCharset = CoreProtocolPNames.HTTP_CONTENT_CHARSET;
     70             String soTimeOut = CoreConnectionPNames.SO_TIMEOUT;
     71             String connectTimeOut = CoreConnectionPNames.CONNECTION_TIMEOUT;
     72             httpClient.getParams().setParameter(contentCharset, "utf-8");
     73             httpClient.getParams().setParameter(soTimeOut, 60000);
     74             httpClient.getParams().setParameter(connectTimeOut, 60000);
     75 
     76             HttpResponse httpResponse = httpClient.execute(httpPost);
     77 
     78             // 获得返回结果
     79             response = EntityUtils.toString(httpResponse.getEntity(), UcopUtils.UCOP_DEFAULT_CHARSET);
     80 
     81             // 获得cookie并存取
     82             cookieStore = httpClient.getCookieStore();
     83             List<Cookie> cookieList = cookieStore.getCookies();
     84             for (Cookie cookie : cookieList){
     85                 String name =cookie.getName();
     86                 String value = cookie.getValue();
     87                 log.info("登陆成功,cookie name = " + name + ", cookie value = " + value);
     88             }
     89             if (log.isInfoEnabled()) {
     90                 log.info("响应内容=" + response);
     91             }
     92             return response;
     93         } catch (Exception e) {
     94             log.error("HttpPost请求失败:" + e.getMessage(), e);
     95             throw new RuntimeException("HttpPost请求失败:" + e.getMessage(), e);
     96         } finally {
     97             if (null != httpClient) {
     98                 httpClient.getConnectionManager().shutdown();
     99             }
    100         }
    101     }
    102 
    103 }

    2.使用CloseableHttpClient类实例化httpClient对象:

     1 package com.zat.ucop.bus.service.impl;
     2 
     3 import com.alibaba.fastjson.JSONObject;
     4 import com.zat.ucop.service.util.UcopCommonUtil;
     5 import com.zat.ucop.service.util.UcopUtils;
     6 import org.apache.http.HttpResponse;
     7 import org.apache.http.NameValuePair;
     8 import org.apache.http.client.CookieStore;
     9 import org.apache.http.client.HttpClient;
    10 import org.apache.http.client.config.RequestConfig;
    11 import org.apache.http.client.entity.UrlEncodedFormEntity;
    12 import org.apache.http.client.methods.HttpPost;
    13 import org.apache.http.cookie.Cookie;
    14 import org.apache.http.impl.client.DefaultHttpClient;
    15 import org.apache.http.impl.client.HttpClients;
    16 import org.apache.http.message.BasicNameValuePair;
    17 import org.apache.http.params.CoreConnectionPNames;
    18 import org.apache.http.params.CoreProtocolPNames;
    19 import org.apache.http.util.EntityUtils;
    20 import org.slf4j.Logger;
    21 import org.slf4j.LoggerFactory;
    22 
    23 import java.util.ArrayList;
    24 import java.util.List;
    25 import java.util.Map;
    26 import java.util.Set;
    27 
    28 public class HttpUtil {
    29 
    30     private static Logger log = LoggerFactory.getLogger(HttpUtil.class);
    31 
    32     /**
    33      * 用来存取cookies信息的变量.
    34      */
    35     private static CookieStore cookieStore;
    36 
    37 
    38     /**
    39      *
    40      * @param url            请求地址
    41      * @param paramMap              请求内容
    42      * @return 响应内容
    43      * @throws Exception
    44      */
    45     public static String requestPostWithCookie(String url, Map<String, Object> paramMap) throws Exception {
    46         if (log.isInfoEnabled()) {
    47             log.info("post请求地址:" + url);
    48             log.info("post请求内容:" + paramMap);
    49         }
    50 
    51         long beginTime = System.currentTimeMillis();
    52         try {
    53             CookieStore cookieStore = new BasicCookieStore();
    54             HttpClient httpClient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
    55             HttpPost httpPost = new HttpPost(url);
    56 
    57             // 设置参数
    58             List<NameValuePair> params = new ArrayList<>();
    59             if (paramMap != null && paramMap.size() > 0) {
    60                 Set<String> keySet = paramMap.keySet();
    61                 for (String key : keySet) {
    62                     params.add(new BasicNameValuePair(key, UcopCommonUtil.convertString(paramMap.get(key))));
    63                 }
    64                 httpPost.setEntity(new UrlEncodedFormEntity(params,"UTF-8"));
    65             }
    66 
    67             // 设置请求和传输超时时间
    68             RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(120000).setConnectTimeout(120000).build();
    69             httpPost.setConfig(requestConfig);
    70 
    71             // 发起请求
    72             HttpResponse response = httpClient.execute(httpPost);
    73 
    74             List<Cookie> cookieList = cookieStore.getCookies();
    75             for (Cookie cookie : cookieList){
    76                 String name =cookie.getName();
    77                 String value = cookie.getValue();
    78                 log.info("cookie name = " + name + ", cookie value = " + value);
    79             }
    80 
    81             // 获取响应数据并转换成JSON
    82             String resString = EntityUtils.toString(response.getEntity(),  UcopUtils.UCOP_DEFAULT_CHARSET);
    83             log.info("post响应内容: " + resString);
    84 
    85             return resString;
    86         } catch (Exception e) {
    87             log.error("请求失败:" + e.getMessage(), e);
    88             throw e;
    89         } finally {
    90             log.info("结束post请求,耗时(ms):" + (System.currentTimeMillis() - beginTime));
    91         }
    92     }
    93 
    94 }
    95     
  • 相关阅读:
    15.scrapy中selenium的应用
    14.UA池和代理池
    17.基于scrapy-redis两种形式的分布式爬虫
    08.Python网络爬虫之图片懒加载技术、selenium和PhantomJS
    05.Python网络爬虫之三种数据解析方式
    06.Python网络爬虫之requests模块(2)
    04.Python网络爬虫之requests模块(1)
    16.Python网络爬虫之Scrapy框架(CrawlSpider)
    10.scrapy框架简介和基础应用
    Python网络爬虫第三弹《爬取get请求的页面数据》
  • 原文地址:https://www.cnblogs.com/caoweixiong/p/10760195.html
Copyright © 2020-2023  润新知