Htmlunit Analog Login Digital Verification code

Source: Internet
Author: User
Tags uuid

The benefits of using Htmlunit are two points, compared to Httpclient,htmlunit is a browser simulation, such as you locate a button, you can execute the Click () method, and do not need to write complex code like in HttpClient, such as heap request The header also has a large number of request parameters, you just need to fill in the user name, password, verification code, as in the use of a browser without interface, of course, more importantly, Htmlunit JS support settings is extremely simple

1. Add Maven's Htmlunit (red) dependency

1<dependencies>2<dependency>3<groupId>junit</groupId>4<artifactId>junit</artifactId>5<version>${junit.version}</version>6<scope>test</scope>7</dependency>8<dependency>9<groupId>com.alibaba</groupId>Ten<artifactId>fastjson</artifactId> One<version>1.2.47</version> A</dependency> -<dependency> -<groupId>org.jsoup</groupId> the<artifactId>jsoup</artifactId> -<version>1.11.3</version> -</dependency> -     <dependency>19 <groupid>net.sourceforge.htmlunit</groupid>20 <artifactId>htmlu nit</artifactid>21 <version>2.18</version>22 </dependency> at      -</dependencies>

2. Ideas

Locate the user name, password, verification code box and other elements, fill it out, the verification code can be downloaded first and then manually input, you can also use tess4j for image recognition, here is the manual input

Testing the site, is a pseudo-ajaxsubmit, testing multiple discovery requires two times to enter the verification code to correctly log in, but the confusion is two times generated the same code (if not the same as the first time you entered an error)

1  Public Static voidMain (string[] args)throwsException {2WebClient WebClient =NewWebClient (browserversion.chrome);3Webclient.getoptions (). setjavascriptenabled (true);4Webclient.getoptions (). setcssenabled (true);5Webclient.getoptions (). Setthrowexceptiononscripterror (false);6     //webclient.getoptions (). Setthrowexceptiononfailingstatuscode (true);7Webclient.getoptions (). Setactivexnative (true);8         9         Ten         //Ajax OneWebclient.setajaxcontroller (NewNicelyresynchronizingajaxcontroller ()); AWebclient.getoptions (). Setuseinsecuressl (false); -          -          the         //Allow redirection -Webclient.getoptions (). setredirectenabled (true); -                  -          +         //Connection timed out -Webclient.getoptions (). SetTimeout (5000); +          A         //js Execution Timeout atWebclient.setjavascripttimeout (10000*3); -          -         //for this website , be sure to open -Webclient.getcookiemanager (). setcookiesenabled (true); -          -String url = "https://www.zuhaowan.com/login/"; inHtmlPage page =webclient.getpage (URL); -Webclient.waitforbackgroundjavascript (5000); to          +          -          the  *HtmlPage NewPage =readypage (page, webClient); $     //String content1 = Newpage.asxml ();Panax Notoginseng     //Ioutils.write (Content1.getbytes (), New FileWriter (New File ("F:/content1.txt" )); -          the         //if the page URL does not change, log back in again +         if(Newpage.geturl (). toString (). Equals (URL)) { ASYSTEM.OUT.PRINTLN ("Please log back in-------------If an error occurs"); theHtmlPage result =readypage (newpage,webclient); +System.out.println ("url----------------" +Result.geturl ()); -System.out.println ("page----" +result.asxml ()); $         //Ioutils.write (Result.asxml (), New FileWriter (New File ("F:/content2.txt" )); $              -         } -          the webclient.close (); -     }Wuyi      the      -      Wu      Public StaticHtmlPage readypage (htmlpage page,webclient WebClient)throwsException { -         //Encapsulate page Elements AboutHtmlForm form = Page.gethtmlelementbyid ("Form2"); $Htmltextinput loginname = Form.getinputbyname ("LoginName"); -Loginname.setvalueattribute ("User name"); -Htmlpasswordinput loginpwd = Form.getinputbyname ("Loginpwd"); -Loginpwd.setvalueattribute ("Password"); A          +         //Verification Code Input Box theHtmltextinput Verify_code = Form.getinputbyname ("Verify_code"); -          $         //Verification Code Picture theHtmlImage verify_img = (htmlimage) Page.getelementbyid ("Verify_img"); theUUID Randomuuid =Uuid.randomuuid (); the         //Save theVerify_img.saveas (NewFile ("./src/main/resources/image/verifyimg" + randomuuid.tostring () + ". png")); -          inSystem.out.println ("Captcha picture Saved!")); theSystem.out.println ("Please enter the verification code"); the         //manually enter the verification code AboutScanner Scanner =NewScanner (system.in); theString Code =scanner.nextline (); theSystem.out.println ("Verification Code-------------" +code); the Verify_code.setvalueattribute (code); +          -      the         //Login ButtonBayiHtmlAnchor login = Page.gethtmlelementbyid ("Loginsubmit"); theHtmlPage NewPage =Login.click (); the          -         //wait for JS to load -Webclient.waitforbackgroundjavascript (5000); the         returnNewPage; the              the     } the     

3. Console partial output

Htmlunit Analog Login Digital Verification code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.