zoukankan      html  css  js  c++  java
  • htmlunit 模拟登录 无验证码

    1.模拟登录csdn,最开始的时候使用的是httpclient,网上的所有模拟登录csdn的版本都是找到lt/execution/event_id.连同用户名及密码

    一起发送即可,但是目前的csdn的登录参数可不止这三个,fkid怎么解决?我抓包时发现fkid是登录时其value才被赋值,奈何找了半天找不到调用的函数,猜测是由js动态生成的,于是转而使用

    htmlunit

     1 public static void main(String[] args) throws Exception {
     2         WebClient client = new WebClient(BrowserVersion.CHROME);
     3         
     4         //允许js
     5         client.getOptions().setJavaScriptEnabled(true);
     6         
     7         //禁用css
     8         client.getOptions().setCssEnabled(false);
     9         
    10         //超时
    11         client.getOptions().setTimeout(5000);
    12         
    13         //js执行超时
    14         client.setJavaScriptTimeout(10000*3);
    15         
    16         //允许重定向
    17         client.getOptions().setRedirectEnabled(true);
    18         
    19         
    20         //必须设置js异常抛出禁止
    21         client.getOptions().setThrowExceptionOnScriptError(false);
    22         
    23         //设置忽略证书
    24         client.getOptions().setUseInsecureSSL(false);
    25         
    26         //设置ajax
    27         client.setAjaxController(new NicelyResynchronizingAjaxController());
    28         
    29         //设置cookie
    30         client.getCookieManager().setCookiesEnabled(true);
    31         
    32         //打开网址
    33         HtmlPage page = client.getPage("https://passport.csdn.net/account/login");
    34         
    35         //等待js加载完全
    36         client.waitForBackgroundJavaScriptStartingBefore(20000);
    37         
    38         HtmlForm form = (HtmlForm) page.getElementById("fm1");
    39         HtmlTextInput username =  form.getInputByName("username");
    40         HtmlPasswordInput password =  form.getInputByName("password");
    41         username.setValueAttribute("18361078119");
    42         password.setValueAttribute("01046818wyc");
    43         
    44         //定位登录按钮(xpath)
    45         HtmlButtonInput button  = (HtmlButtonInput) page.getByXPath("//input[(@class='logging')]").get(0);
    46          
    47         HtmlPage retPage = button.click();
    48        // 等待JS驱动dom完成获得还原后的网页  
    49        client.waitForBackgroundJavaScript(1000);  
    50        //输出url
    51        System.out.println(retPage.getUrl().toString());   
    52        //输出网页的内容
    53        System.out.println(retPage.asXml());
    54 
    55         client.close();
    56         
    57     }
  • 相关阅读:
    Binder机制,从Java到C (5. IBinder对象传递形式)
    Binder机制,从Java到C (4. Parcel)
    Binder机制,从Java到C (3. ServiceManager in Java)
    Binder机制,从Java到C (2. IPC in System Service :AMS)
    Binder机制,从Java到C (10. Binder驱动)
    Binder机制,从Java到C (8. ServiceManager in Native)
    Binder机制,从Java到C (7. Native Service)
    Binder机制,从Java到C (6. Binder in Native : libbinder)
    Binder机制,从Java到C (1. IPC in Application Remote Service)
    SVM详解
  • 原文地址:https://www.cnblogs.com/tele-share/p/9445419.html
Copyright © 2011-2022 走看看