Selinium登录系统cookies的重复使用

1Webdriver获取当前登录的Cookies

1 public static Set<Cookie> cookies = new HashSet<Cookie>();
2 cookies = driver.manage().getCookies();
3 System.out.println("Cookies = " + cookies);

2Jsoup使用获取的Cookies访问系统

1 Map<String, String> ck = new HashMap<String, String>();
2 Connection conn = Jsoup.connect("http://dealer.group.che168.com");
3 for(Cookie cookie:cookies){
4     ck.put(cookie.getName(), cookie.getValue());
5 }        
6 conn.cookies(ck);
7 conn.timeout(30000);
8 Document doc = conn.get();

3、HttpClient使用获取的Cookies访问系统

 1 public class HttpMethod {
 2     private CloseableHttpClient httpClient = HttpClients.createDefault();
 3     private HttpContext httpContext = new BasicHttpContext();
 4     private HttpClientContext context = HttpClientContext.adapt(httpContext);
 5 
 6     //以下方法的参数Set<Cookie> cookies即为Selenium中获取的Cookies
 7     public String get(String url, Set<Cookie> cookies){
 8         CookieStore cookieStore = new BasicCookieStore(); 
 9         for(Cookie cookie : cookies){
10             BasicClientCookie bc = new BasicClientCookie(cookie.getName(),cookie.getValue());
11             bc.setPath("/");
12             bc.setDomain(".che168.com");//Domain的值可通过打印Webdriver的Cookies获得
13             bc.setVersion(0);
14             cookieStore.addCookie(bc);
15         } 
16         context.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
17        CloseableHttpResponse response = null;
18        String content = null;
19        try {
20            HttpGet get = new HttpGet(url);
21            //设置httpGet的头部参数信息   
22             get.setHeader("Accept", "text/html, application/xhtml+xml, */*");  
23             get.setHeader("Accept-Encoding", "gzip, deflate");  
24             get.setHeader("Accept-Language", "en-US");  
25             get.setHeader("User-Agent", "User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");  
26             
27         response = httpClient.execute(get, context);
28         System.out.println("response = " + response);            
29         HttpEntity entity = response.getEntity();
30         content = EntityUtils.toString(entity);
31         System.out.println("content = " + content);        
32         EntityUtils.consume(entity);//关闭
33         return content;
34     } catch (Exception e) {
35         e.printStackTrace();
36         if (response != null) {
37             try {
38                 response.close();
39             } catch (IOException e1) {
40                 e1.printStackTrace();
41             }
42         }
43     }
44     return content;
45     }
46 }

代码中调用该方法:

1 String pageSource = HttpMethod.get("http://dealer.group.che168.com", cookies);

HttpClient访问系统的代码参照网址:https://my.oschina.net/ChiLin/blog/759601

转载于:https://www.cnblogs.com/dingziyin/p/7358559.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值