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

  • 相关阅读:
    【centos】centos中添加一个新用户,并授权
    linux基础(10)-导航菜单
    linux基础(9)-获取时间
    linux基础(8)-颜色显示
    linux基础(8)-文件处理(awk 、sed、grep)
    linux基础(7)-IO重定向
    linux基础(6)-shell编程
    linux基础(5)-用户及权限
    linux基础(4)-常用命令
    linux基础(3)-java安装
  • 原文地址:https://www.cnblogs.com/dingziyin/p/7358559.html
Copyright © 2011-2022 走看看