zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    BZOJ-2743: [HEOI2012]采花(树状数组 or TLE莫队)
    BZOJ-1122: [POI2008]账本BBB (单调栈神题)
    2017年10月18日23:54:18
    [校内自测 NOIP模拟题] chenzeyu97要请客(单调栈)
    BZOJ-1057: [ZJOI2007]棋盘制作(单调栈)
    [校内自测] 奶牛编号 (递推+智商)
    [校内自测] Incr (LIS+智商)
    BZOJ1486 [HNOI2009]最小圈
    BZOJ2400 Spoj 839 Optimal Marks
    BZOJ2595 [Wc2008]游览计划
  • 原文地址:https://www.cnblogs.com/dingziyin/p/7358559.html
Copyright © 2011-2022 走看看