About Verification Code Login

Source: Internet
Author: User


Some Web sites are crawled and some need to be logged in to gain access. If just need to login, here can recommend you a tool, very useful

In Firefox browse there is a plugin firebug (need to install), through this plugin can be detailed to see the site's access process (link jump and Access order), as well as each link of the request header information, response header information, but also can view the post submitted data. Of course, there are some development tools in IE and Google Browser, F12 directly call out, but the personal feeling of Firefox firebug more useful, ie and Google, I also occasionally use.

With the tools described above, it is easy to get a detailed simulation of the process and then simulate the login.

Here I am the introduction is the login if you need verification code, there is some trouble, I think of a solution here, more commonly used, is pop-up verification code

This is achieved by simulating the login

Public class loginbycode {public static void main (String[] args)  { Closeablehttpclient httpclient = httpclientbuilder.create (). build (); Simpledateformat format = new simpledateformat ("Yyyymmddhhmmss"); string path =  "d:/img/tmp/"  + format.format (New date ())  +  ". jpg"; try  {String imgurl =  "http://www.shanghaiip.cn/wasWeb/login/Random.jsp"; Httpurirequest get = new httpget (Imgurl); Httpresponse res = httpclient.execute (get); Res.setheader ("Content-type",  "Image/gif"); byte [] img = entityutils.tobytearray (Res.getentity ());//Download Verification code Picture SaveFile (PATH,&NBSP;IMG); String code = new imgdialog (). ShowDialog (Null, path);//pop-up verification code, get the verification code to fill String login  =  "http://www.shanghaiip.cn/wasWeb/login/loginServer.jsp"; Httppost post = new httppost (login); List<namevaluepair> data = new arraylist<namevaluepair> ();d Ata.add (New basicnamevaluepair (" Username ", " zhpatent "));d Ata.add (New basicnamevaluepair (" Password ", " 5ca072839350b0733a2a456cc4004371 ")//Firefox firebug can see the password is encrypted Data.add (New basicnamevaluepair (" Newrandom ",  code));p ost.setentity (new urlencodedformentity (data)); Res = httpclient.execute (POST); Header[] headers = res.getheaders ("location");//Get Jump link get = new httpget (headers[ 0].getvalue ()); Res = httpclient.execute (get); String body = entityutils.tostring (Res.getentity ());if  (Body.contains ("Zhpatent"))  { SYSTEM.OUT.PRINTLN ("Emulation login succeeded:"  + body.substring (Body.indexof ("Zhpatent")  - 40,  Body.indexof ("Zhpatent")  + 40));}}  catch  (exception e)  {system.out.println ("Exception:"  + e.getmessage ());}  finally {file file = new file (Path);if  (File. exists ())  {file.delete ();} Try {httpclient.close ();}  catch  (ioexception e)  {e.printstacktrace ();}} Private static void savefile (String path, byte[] data)  {int size  = 0;byte[] buffer = new byte[10240];try  (bufferedoutputstream bos =  new bufferedoutputstream (New fileoutputstream (path)); Bytearrayinputstream is = new bytearrayinputstream (data)  {while  (size =  is.read (buffer))  != -1)  {bos.write (buffer, 0, size);}}  catch  (ioexception e)  {e.printstacktrace ();}}


Verification Code Tool Class

Public class imgdialog {public string message = null;private jbutton  confirm;private JDialog dialog = null;private TextField field; string result =  "";p Ublic string showdialog (Jframe father, string path )  {jlabel label = new jlabel (); Label.setborder (New etchedborder ( Etchedborder.lowered, null, null)); Label.setbounds (10, 10, 125, 51); Label.setIcon (new  imageicon (path)); Field = new textfield (); Field.setbounds (145,&NBSP;10,&NBSP;65,&NBSP;20); Confirm = new jbutton ("OK"); Confirm.setbounds (145,&NBSP;40,&NBSP;65,&NBSP;20); Confirm.addactionlistener (New actionlistener ()  {@Overridepublic  void actionperformed ( actionevent e)  {result = field.gettext (); ImgDialog.this.dialog.dispose ();}); Dialog = new jdialog (father, true);d ialog.settitle ("Please enterThe verification code in the image "); Container pane = dialog.getcontentpane ();p ane.setlayout (null);p ane.add (label);p ane.add (field); Pane.add (confirm);d Ialog.pack ();d ialog.setsize (New dimension (235, 110));d ialog.setlocation (750,  430);//dialog.setlocationrelativeto (father);d ialog.setvisible (true); return result;}}


The experimental results are as follows

Run will download the verification code and pop up

Enter the verification code to get my user information in the page that jumps after login.

I am here to use the HttpClient analog login,httpclient do not have to manage cookies, so it is convenient to use, there will be no verification code on the issue of the number.

If you are using Jsoup to log in a little bit of trouble, you have to manage the cookies, access to the Verification code page at the same time to download the verification code and to get cookies, and then when the simulation login need to bring cookies

About Verification Code Login

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.