Resources Download links (resources free, heavy share)
tesseract:http://download.csdn.net/detail/chenyangqi/9190667
jai_imageio-1.1-alpha,swingx-1.0:http://download.csdn.net/detail/chenyangqi/9190683
HttpWatch professional:http://download.csdn.net/detail/chenyangqi/9208339
Httpclient.jar Codec.jar logging.jar:http://download.csdn.net/detail/chenyangqi/9208315
Brief introduction:
Simulation of the following landing page, you need to master the HttpWatch use method
Ocr-tesseract How to identify the verification code can refer to my other blog http://www.cnblogs.com/chenyangqi/p/4906282.html
One:
Use Eclipse to build a project that introduces several jar packages to be used by Ocr-tesseract and httpclient (the jar package already provides a download link at the beginning of the article)
Project ideas:
1: First HTTP request captcha URL (typically get request) download Captcha to local, identify by ocr-tesseract, return verification code, and save cookie
Here is the request grab for the captcha URL in HttpWatch, just use the Get and URL addresses.
2: The second HTTP request login URL (typically a POST request) sends the data in the PostData column in HttpWatch. Synchronizes cookies and returns InputStream data
3: The request succeeds, matches the returned InputStream data, and if there is a match, the login succeeds.
This is the return of the data and the expected is the same, the data returned in the HttpWatch content can be seen, compared to the success and failure of the request, you can use substring to see if there is a match, or a regular expression
Second: Difficult to solve
The most important thing to achieve landing is the verification code identification and cookie synchronization problem, verification code identification in my other blog has been resolved http://www.cnblogs.com/chenyangqi/p/4906282.html
So only the cookie problem, before using httpconnection always cookie is not synchronized, the back of the httpclient (use httpclient need to apply Httpclient.jar Codec.jar Logging.jar three jar packages, which can be downloaded at the beginning of the article), HttpClient objects can be managed well
Cookies and request headers. So the cookie read and synchronization, we do not need to operate, as long as the guarantee is the same HttpClient object is OK, so the definition of httpclient as a global variable is better, but also to avoid multithreading caused httpclient object changes, Should be a change to the HttpClient object the cookie changes.
Unable to sync cookies causes login failure.
Three: Code implementation:
Packagecom.cyq.request;ImportJava.io.BufferedWriter;ImportJava.io.File;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;ImportJava.io.FileWriter;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.PrintStream;ImportJava.text.SimpleDateFormat;Importjava.util.ArrayList;ImportJava.util.Scanner;Importorg.apache.commons.httpclient.HttpClient;ImportOrg.apache.commons.httpclient.NameValuePair;ImportOrg.apache.commons.httpclient.methods.GetMethod;ImportOrg.apache.commons.httpclient.methods.PostMethod; Public classRequestmain {StaticHttpClient client =NewHttpClient (); Public Static voidMain (string[] args) {Init (); } Private Static voidInit () { getImage ("User name", "password"); } Private Static voidgetImage (String name_get, String password_get) {GetMethod get=NewGetMethod ("Verification Code URL address"); Try{Client.executemethod (GET); File StoreFile=NewFile ("D:/verifycode.jpg");//Verification Code staging local location FileOutputStream output=NewFileOutputStream (StoreFile); InputStream is=Get.getresponsebodyasstream (); FileOutputStream Fos=NewFileOutputStream (StoreFile); byte[] B =New byte[1024]; while((Is.read (b))! =-1) {fos.write (b); } is.close (); Fos.close (); String Codeval=GetCode (); Try{String Code= codeval.substring (0, Codeval.indexof ("\ n")-1); Postrequest (Name_get, Password_get, code); } Catch(Exception e) {System.out.println ("E" + name_get + "Captcha Error"); } } Catch(IOException e) {e.printstacktrace (); } } Private Static voidpostrequest (String name_data, String password_data, String code) {Postmethod Postmethod=NewPostmethod ("POST request URL address when logging in"); namevaluepair[] Data= {NewNamevaluepair ("act", "add"), NewNamevaluepair ("name", Name_data),NewNamevaluepair ("Password", Password_data),NewNamevaluepair ("Verify", Code)}; Postmethod.setrequestbody (data); Try{Client.executemethod (postmethod);String Text =postmethod.getresponsebodyasstring (); //matches a matching string in the result set if(Text.indexof ("self.location="). /.. /.. /MODULE/WEBCONTENT/WEB/INDEX_SELF.JSF ") >= 0) {Result_data.add ("User name:" +name_data+ "Password:" +password_data); Writeresulttofile ("User name:" +name_data+ "Password:" +password_data); System.out.println ("User name" + name_data + "Success"); } Else{System.out.println ("User name" + name_data + "failed"); } } Catch(IOException e) {e.printstacktrace (); } } Private Staticstring GetCode () {string Valcode=NULL; String Path= "D://verifycode.jpg"; Try{Valcode=NewOCR (). Recognizetext (NewFile (path), "JPG"); } Catch(IOException e) {e.printstacktrace (); } Catch(Exception e) {e.printstacktrace (); } returnValcode; }}
Okay, over,.
Disclaimer: The blog post for the original, reproduced please indicate the source
This program is only used for learning, do not use this content to engage in illegal activities and violent cracking activities
Java analog HTTP login with verification code