Httpclient simulates login to Renren's network and sends a mood (screen flushing)

Source: Internet
Author: User

The logon website is 3g.renren.com because it is a mobile phone for everyone, the page is relatively simple

First, use httpget to retrieve the "http://3g.renren.com" HTML code, is to use jsoup parsing the login form, including the verification code of the image URL

Because you cannot bypass the verification code, you can use the URL of the verification code to construct an image.

Httpget must be used to build the image and ImageIO. Read (new URL (URL) is used. In this way, the session is not managed in the httpclient instance.

If you do not want to write it, put it all in the comments and directly add the code.

Because the program relies heavily on the HTML source code, the HTML code may not be used when the front-end of everyone changes it.

Package COM. renren. main; import Java. AWT. graphics; import Java. AWT. image; import Java. AWT. event. actionevent; import Java. AWT. event. actionlistener; import Java. AWT. image. bufferedimage; import Java. io. ioexception; import Java. io. inputstream; import Java. util. arraylist; import Java. util. hashmap; import Java. util. list; import Java. util. map; import javax. imageIO. imageIO; import javax. swing. jbutton; import javax. swing. Jframe; import javax. swing. jpanel; import javax. swing. jpasswordfield; import javax. swing. jtextarea; import javax. swing. jtextfield; import Org. apache. HTTP. httpentity; import Org. apache. HTTP. httpresponse; import Org. apache. HTTP. httpstatus; import Org. apache. HTTP. namevaluepair; import Org. apache. HTTP. client. clientprotocolexception; import Org. apache. HTTP. client. httpclient; import Org. apache. HTTP. client. respons Ehandler; import Org. apache. HTTP. client. entity. urlencodedformentity; import Org. apache. HTTP. client. methods. httpget; import Org. apache. HTTP. client. methods. httppost; import Org. apache. HTTP. impl. client. basicresponsehandler; import Org. apache. HTTP. impl. client. defaulthttpclient; import Org. apache. HTTP. message. basicnamevaluepair; import Org. apache. HTTP. protocol. HTTP; import Org. jsoup. jsoup; import Org. jsoup. node S. document; import Org. jsoup. nodes. element; import Org. jsoup. select. elements; public class login extends jframe implements actionlistener {private jtextfield email; private jpasswordfield password; private jtextfield verifycode; private jbutton login; private imageboxpanel imagebox; private image; private msgbox; private Final httpclient client; private httppost post; private httpget get; private HTTP Response response; private responsehandler <string> responsehandler; private Map <string, string> form_map; private Boolean flag; // whether there is a verification code private string HTML; Public login () {super ("Everyone login"); client = new defaulthttpclient (); responsehandler = new basicresponsehandler (); form_map = new hashmap <string, string> (); object OBJ; setlayout (null); setdefaclocloseoperation (exit_on_close); setresizable (false); email = new Jtextfield ("<email>"); Password = new jpasswordfield ("<password>"); verifycode = new jtextfield (); login = new jbutton ("login"); login. addactionlistener (this); html = view ("http://3g.renren.com"); Init (); try {imagebox = new imageboxpanel (createbufferedimage ();} catch (exception e) {e. printstacktrace (); imagebox = new imageboxpanel ();} // layoutthis. setbounds (500,300,280,220); email. setbounds (10, 10,250, 3 0); password. setbounds (10, 50,250, 30); verifycode. setbounds (10, 90,150, 30); imagebox. setbounds (205, 80, 54, 46); login. setbounds (10,130,250, 30); add (email); add (password); add (verifycode); add (imagebox); add (LOGIN); setvisible (true );} private bufferedimage createbufferedimage () {inputstream = NULL; try {string source = view ("http://3g.renren.com"); string url = getverifycodeurl (source); If ("err Or ". Equals (URL) {return NULL;} // rndimg? Post = _ requestfriend_de6073b242a6fc34b67d228abf982916 & RND = 1335096399071 // analyze the logon form to find that if a login requires a verification code, the form contains verifykey and verifycode // while the verifykey value is exactly a part of the Verification Code address (32 characters in the middle), so the verifykey is retrieved from string key = URL; key = key. replaceall ("HTTP [\ D] * Nd _", ""); Key = key. replaceall ("& [\ D] *", ""); form_map.put ("verifykey", key); // update verifykey, which corresponds to the system of the current verification code. out. println (key); get = new httpget (URL); response = client.exe Cu Te (get); If (httpstatus. SC _ OK = response. getstatusline (). getstatuscode () {// The following two if values are obtained online: httpentity entity = response. getentity (); If (entity! = NULL) {inputstream = entity. getcontent (); // an inputstream is returned, but get is called in finally. after abort (), it seems that it will become null, and there is no way, so directly construct bufferedimage and return ImageIO. read (inputstream) ;}} catch (exception e) {e. printstacktrace ();} finally {Get. abort ();} return NULL;}/*** obtain the HTML code of a URL * @ Param URL * @ return */private string view (string URL) {string HTML; try {Get = new httpget ("http://3g.renren.com/"); html = client.exe C Ute (get, responsehandler);} catch (exception e) {e. printstacktrace (); html = "error";} finally {Get. abort ();} return HTML;}/*** obtain the address of the Verification Code image * @ Param Source Page source code * @ return */private string getverifycodeurl (string source) {string URL; flag = true; try {document DOC = jsoup. parse (source); // The analysis form shows that this sentence is available. However, if you use this method to make a difference, this program may not use Element E = Doc. getelementsbyattributevaluecontaining (" Lt "," Here is the Verification Code "). get (0); url = E. ATTR ("src"); url = "http://3g.renren.com" + URL;} catch (exception e) {// The exception information is printed, but it looks uncomfortable, delete it. // generally, if there is no verification code, the above get (0) will certainly not work. system. out. println ("no verification code ~ "); Url =" error "; flag = false; // indicates whether there is a verification code. Whether the verifykey and verifycode attributes are passed through the form} return URL ;} private void Init () {string html = view ("http://3g.renren.com"); document DOC = jsoup. parse (HTML); // retrieve the 3g.ren.com code element form = Doc. getelementsbytag ("form "). get (0); string action = form. ATTR ("action"); form_map.put ("action", action); elements es = form. getelementsbytag ("input"); For (Element E: ES) {form_map. Put (E. ATTR ("name"), E. ATTR ("value") ;}@overridepublic void actionreceivmed (actionevent e) {login (); system. out. println (View ("http://3g.renren.com"); // After the login is successful, display the mood of the interface, mainly to achieve a function, did not take into account other aspects // including whether the login is successful ah, whether the sending mood is successful or not. // However, this is quite simple. It is nothing more than parsing the post HTML code to see if any error message box = new msgbox ();} private Boolean login () {post = new httppost (form_map.get ("action"); // It is said that some websites will be filtered out if no header is set, after all, everyone is still a pretty big website. Add post. sethead Er ("User-Agent", "Mozilla/5.0 (windows; U; Windows NT 5.1; en-US) applewebkit/534.3 (khtml, like gecko) chrome/6.0.472.63 Safari/534.3 "); List <namevaluepair> NVP = new arraylist <namevaluepair> (); NVP. add (New basicnamevaluepair ("origurl", form_map.get ("origurl"); NVP. add (New basicnamevaluepair ("lbskey", form_map.get ("lbskey"); NVP. add (New basicnamevaluepair ("C", form_map.get ("C"); NVP. add (New BAS Icnamevaluepair ("Ref", form_map.get ("Ref"); NVP. add (New basicnamevaluepair ("email", email. gettext (); NVP. add (New basicnamevaluepair ("password", new string (password. getPassword (); NVP. add (New basicnamevaluepair ("PQ", form_map.get ("PQ"); If (FLAG) {NVP. add (New basicnamevaluepair ("verifycode", verifycode. gettext (); NVP. add (New basicnamevaluepair ("verifykey", form_map.get ("verifykey");} // System. Out. println (form_map.get ("verifykey"); try {post. setentity (New urlencodedformentity (NVP, HTTP. utf_8); // response = client.exe cute (post);} catch (exception e) {e. printstacktrace (); Return false;} finally {post. abort ();} return true;} public static void main (string [] ARGs) {login Renren = New Login (); // system. out. println (Renren. view ("http://3g.renren.com");} class msgb Ox extends jframe implements actionlistener {jtextarea MSG; jbutton submit; // the number of times a mood is sent. I wanted to screen a screen, but it is not very powerful, jtextfield time with sleep enabled for a period of time; Public msgbox () {setlayout (null); setresizable (false); setbounds (500,500,365,125); MSG = new jtextarea (); submit = new jbutton ("Send ~ "); Time = new jtextfield (" 1 "); MSG. setbounds (10, 10,250, 80); time. setbounds (270, 10, 81, 40); Submit. setbounds (270, 50, 80, 40); Submit. addactionlistener (this); add (MSG); add (submit); add (time); setvisible (true) ;}@ overridepublic void action=med (actionevent E) {for (INT I = 0; I <integer. parseint (time. gettext (); I ++) {post = new httppost ("http://3g.renren.com/status/wUpdateStatus.do"); // All are viewed from the first logon, and then some information is captured post. setheader ("User-Agent", "Mozilla/5.0 (windows; U; Windows NT 5.1; en-US) applewebkit/534.3 (khtml, like gecko) chrome/6.0.472.63 Safari/534.3 "); List <namevaluepair> NVP = new arraylist <namevaluepair> (); NVP. add (New basicnamevaluepair ("_ SCN", "XXXXXXXX"); // you do not know how to generate this. Each ID may correspond to a NVP. add (New basicnamevaluepair ("sour", ""); NVP. add (New basicnamevaluepair ("loginbybm", ""); NVP. add (New basicnamevaluepair ("status", MSG. gettext () + I); // others do not matter, but NVP remains. add (New basicnamevaluepair ("PID", ""); NVP. add (New basicnamevaluepair ("empty", "1"); try {post. setentity (New urlencodedformentity (NVP, HTTP. utf_8); response = client.exe cute (post); system. out. println (response); thread. sleep (1000l); // If sleep is not set, it should be filtered out by everyone. The specific time interval can be sent once and has not been tested, however, it is estimated that 500 milliseconds is okay.} catch (exception E1) {e1.printstacktrace ();} finally {post. abort () ;}}}} class imageboxpanel extends jpanel {private image; Public imageboxpanel (image) {This. image = image;} public imageboxpanel () {}@ overrideprotected void paintcomponent (Graphics g) {G. drawimage (image, 0, 0, 54, 46, null );}}

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.