Iqq Study Notes 2: Use Sina Weibo to enter the verification code and remotely control the exit

Source: Internet
Author: User

Iqq learning notes
This document is for study purposes only and shall not be used for any illegal or infringing purposes.

Please indicate the original position of the post:
Http://xuekaiyuan.com/forum.php? MoD = viewthread & tid = 5

Add QQ group: 306320259 for discussion

Iqq Study Notes 2 Note: Use Sina Weibo to enter the verification code and remotely control the exit

In the 1st cases, iqq login, verification code, and message receiving were implemented. Two of them require manual participation. The first is to open the verification code image, and then enter the verification code, the second step is to exit the program forcibly. The Verification Code cannot be automatically recognized yet, but the interaction mode can be improved. In this example, the image of the verification code and the verification code are displayed on Sina Weibo. After you exit the program, change it to the QQ message and follow the message content.

Iqq Study Notes 2 Program: using Sina Weibo to enter the verification code, remote control to exit

This is the main program. Replace the username constant value with the QQ number, and the password constant value with the QQ password corresponding to the QQ number.

package test_2;import java.util.logging.Logger;import iqq.im.QQClient;import iqq.im.QQException;import iqq.im.QQNotifyListener;import iqq.im.WebQQClient;import iqq.im.actor.ThreadActorDispatcher;import iqq.im.bean.QQMsg;import iqq.im.bean.QQStatus;import iqq.im.bean.content.ContentItem;import iqq.im.bean.content.TextItem;import iqq.im.event.QQActionEvent;import iqq.im.event.QQActionFuture;import iqq.im.event.QQNotifyEvent;import iqq.im.event.QQNotifyEventArgs;import iqq.im.event.QQActionEvent.Type;public class Test_2 {private static final String username = "**********";private static final String password = "**********";private static final Logger logger = java.util.logging.Logger.getLogger("");private static QQClient client;public static void main(String[] args) {client = new WebQQClient(username, password, new QQNotifyListener() {@Overridepublic void onNotifyEvent(QQNotifyEvent event) {if (event.getType() == QQNotifyEvent.Type.CHAT_MSG) {if (event.getTarget() instanceof QQMsg) {QQMsg msg = (QQMsg) event.getTarget();for (ContentItem contentItem : msg.getContentList()) {if (contentItem instanceof TextItem) {TextItem textItem = (TextItem) contentItem;logger.info(textItem.getContent());if (textItem.getContent().startsWith("logout ")) {client.logout(null);}}}}} else if (event.getType() == QQNotifyEvent.Type.CAPACHA_VERIFY) {if (event.getTarget() instanceof QQNotifyEventArgs.ImageVerify) {QQNotifyEventArgs.ImageVerify imageVerify = (QQNotifyEventArgs.ImageVerify) event.getTarget();String code = ImageVerifyQuestioners.ImageVeiryQuestion(imageVerify);client.submitVerify(code, event);} else {logger.info(event.getTarget().getClass().getName());}} else {logger.info("TODO QQNotifyEvent: " + event.getType() + ", " + event.getTarget());}}}, new ThreadActorDispatcher());QQActionFuture future = client.login(QQStatus.ONLINE, null);try {QQActionEvent event = future.waitFinalEvent();if (event.getType() == Type.EVT_OK) {client.beginPollMsg();}} catch (QQException e) {e.printStackTrace();}}}

To facilitate the input of multiple image verification codes, an image Verification Code interface is designed.

package test_2;import iqq.im.event.QQNotifyEventArgs.ImageVerify;public abstract class ImageVerifyQuestioner {private ImageVerify imageVerify;private String Code;public ImageVerifyQuestioner(ImageVerify imageVerify) {this.imageVerify = imageVerify;this.Code = "";}protected final ImageVerify getImageVerify() {return imageVerify;}public final String getCode() {return Code;}protected final void setCode(String code) {Code = code;}public abstract void loop ();}

You can use a scheduler to manage the input methods of multiple image verification codes. When you obtain the verification code in one input mode, other input methods exit.

package test_2;import java.util.ArrayList;import java.util.List;import iqq.im.event.QQNotifyEventArgs.ImageVerify;public class ImageVerifyQuestioners {public static String ImageVeiryQuestion (ImageVerify imageVerify) {String code = "";List<ImageVerifyQuestioner> imageVerifyQuestionerList = new ArrayList<ImageVerifyQuestioner>();imageVerifyQuestionerList.add(new ImageVerifyConsoleQuestioner(imageVerify));imageVerifyQuestionerList.add(new ImageVerifyWeiboQuestioner(imageVerify));boolean alive = true;while (alive) {for(ImageVerifyQuestioner imageVerifyQuestioner : imageVerifyQuestionerList) {if (true == alive) {imageVerifyQuestioner.loop();if (imageVerifyQuestioner.getCode() != "") {code = imageVerifyQuestioner.getCode();alive = false;}}}try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}}return code;}}

Maintain the input mode of the original command line Interactive input image verification code.

/*****/Package test_2; import Java. io. file; import Java. io. ioexception; import javax. imageIO. imageIO; import iqq. im. event. qqpolicyeventargs. imageverify; public class extends imageverifyquestioner {private Boolean isprompt; private stringbuilder codestringbuilder = new stringbuilder (); Public evaluate (imageverify) {super (imageverify);} private void prompt () {try {ImageIO. write (this. getimageverify (). image, "PNG", new file ("verify.png"); system. out. println (this. getimageverify (). reason); system. out. print ("Enter the verification code in the verify.png image in the project root directory:"); isprompt = true;} catch (ioexception e) {e. printstacktrace () ;}} private void read () {try {int available = system. in. available (); If (0 <available) {int inchar = system. in. read (); Switch (character. getType (inchar) {Case character. uppercase_letter: Case character. lowercase_letter: Case character. decimal_digit_number: codestringbuilder. append (char) inchar); break; Case character. control: This. setcode (codestringbuilder. tostring (); break ;}} catch (ioexception e) {e. printstacktrace () ;}@ overridepublic void loop () {If (false = This. isprompt) {This. prompt ();} If (this. getcode () = "") {This. read ();}}}

The Input Method for entering the image verification code through Sina Weibo is added. The value of accesstokenfile should be replaced with the path for saving the Sina Weibo access token.

/*****/Package test_2; import Java. io. bufferedreader; import Java. io. bytearrayoutputstream; import Java. io. filenotfoundexception; import Java. io. filereader; import Java. io. ioexception; import Java. io. inputstreamreader; import Java. io. outputstream; import java.net. malformedurlexception; import java.net. URL; import java.net. urlconnection; import Java. text. simpledateformat; import Java. util. date; import Java. util. Logging. logger; import javax. imageIO. imageIO; import javax.net. SSL. httpsurlconnection; import Org. JSON. jsonobject; import iqq. im. event. qqpolicyeventargs. imageverify; public class imageverifyweiboquestioner extends imageverifyquestioner {Private Static final string accesstokenfile = "**********"; Private Static final logger = Java. util. logging. logger. getlogger (""); Private string accesstoken; priva Te long weiboid; private long lastchecktime; Public verify (imageverify) {super (imageverify);} private void readaccesen en () {filereader failed; try {allow = new filereader (accesstokenfile ); bufferedreader bufferedreaderaccesstoken = new bufferedreader (filereaderaccesstoken); accesstoken = bufferedreaderaccesstoken. readline (); bufferedreader Accesstoken. close (); filereaderaccesen en. close ();} catch (filenotfoundexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace () ;}} private void uploadverifyimage () {try {// statusstringbuilder = new stringbuilder (); statusstringbuilder. append ("Please @ Hu zhenghui enter the Verification Code"); statusstringbuilder. append (New simpledateformat ("yyyy-mm-dd hh: mm: SS ")). format (new date (); // urlst Ringbuilder urlstringbuilder = new stringbuilder (); urlstringbuilder. append ("https://api.weibo.com/2/statuses/upload.json? Access_token = "); urlstringbuilder. append (accesstoken); Url url = new URL (urlstringbuilder. tostring (); urlconnection = URL. openconnection (); If (urlconnection instanceof httpsurlconnection) {string boundary = "-------------------------" + String. valueof (system. currenttimemillis (); bytearrayoutputstream = new bytearrayoutputstream (); bytearrayoutputstream. write (("--" + Boundary + "\ r \ n "). getbytes (); bytearrayoutputstream. write ("content-Disposition: Form-data; name = \" pic \ "; filename = \" verifyimage.png \ "\ r \ n "). getbytes (); bytearrayoutputstream. write ("Content-Type: image/PNG \ r \ n "). getbytes (); bytearrayoutputstream. write ("\ r \ n "). getbytes (); ImageIO. write (this. getimageverify (). image, "PNG", bytearrayoutputstream); bytearrayoutputstream. write ("\ r \ n "). getbytes (); B Ytearrayoutputstream. write ("--" + boundary + "\ r \ n "). getbytes (); bytearrayoutputstream. write ("content-Disposition: Form-data; name = \" status \ "\ r \ n "). getbytes (); bytearrayoutputstream. write ("\ r \ n "). getbytes (); bytearrayoutputstream. write (statusstringbuilder. tostring (). getbytes (); bytearrayoutputstream. write ("\ r \ n --" + boundary + "-- \ r \ n "). getbytes (); httpsurlconnection = (HTTP Surlconnection) urlconnection; httpsurlconnection. setrequestmethod ("Post"); httpsurlconnection. addrequestproperty ("Content-Type", "multipart/form-data; boundary =" + boundary); httpsurlconnection. setdooutput (true); httpsurlconnection. setdoinput (true); outputstream = httpsurlconnection. getoutputstream (); outputstream. write (bytearrayoutputstream. tobytearray (); outputstream. flush (); outputstream. Close (); stringbuilder response = new stringbuilder (); inputstreamreader = new inputstreamreader (httpsurlconnection. getinputstream (); int readchar = inputstreamreader. Read (); While (-1! = Readchar) {response. append (char) readchar); readchar = inputstreamreader. read ();} inputstreamreader. close (); httpsurlconnection. disconnect (); logger.info (response. tostring (); jsonobject responsejson = new jsonobject (response. tostring (); this. weiboid = responsejson. getlong ("ID"); logger.info ("Weibo id =" + String. valueof (this. weiboid);} catch (filenotfoundexception e) {e. printstacktrace ();} catch (Ioexception e) {e. printstacktrace () ;}} private void checkcomment () {try {stringbuilder commentstringbuilder = new stringbuilder (); commentstringbuilder. append ("https://api.weibo.com/2/comments/show.json? Filter_by_author = 1 & access_token = "); commentstringbuilder. append (accesstoken); commentstringbuilder. append ("& id ="); commentstringbuilder. append (weiboid); Url comment = new URL (commentstringbuilder. tostring (); urlconnection commentconnection = comment. openconnection (); If (commentconnection instanceof httpsurlconnection) {httpsurlconnection commenthttpsurlconnection = (httpsurlconnection) commentconnectio N; commenthttpsurlconnection. setdoinput (true); inputstreamreader commentstreamreader = new inputstreamreader (commenthttpsurlconnection. getinputstream (); stringbuilder commentresponse = new stringbuilder (); int commentchar = commentstreamreader. read (); While (-1! = Commentchar) {commentresponse. append (char) commentchar); commentchar = commentstreamreader. read ();} commentstreamreader. close (); commenthttpsurlconnection. disconnect (); jsonobject commentsjson = new jsonobject (commentresponse. tostring (); int total_number = commentsjson. getint ("total_number"); logger.info ("total_number =" + String. valueof (total_number); If (0 <total_number) {jsonobject commentjson = Commentsjson. getjsonarray ("Comments "). getjsonobject (0); logger.info ("text =" + commentjson. getstring ("text"); this. setcode (commentjson. getstring ("text") ;}} catch (malformedurlexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace () ;}@ overridepublic void loop () {If (null = accesstoken) {This. readaccesstoken ();} If (null! = Accesstoken) & (0 = weiboid) {This. uploadverifyimage (); lastchecktime = system. currenttimemillis ();} If (0 <weiboid) & (this. getcode () = "") & (10000 <system. currenttimemillis ()-lastchecktime) {This. checkcomment (); lastchecktime = system. currenttimemillis ();}}}

Iqq Study Notes 2 test: using Sina Weibo to enter the verification code, remote control to exit
To test this program, you need to register two QQ numbers and add them as friends. Keep one QQ account logged on from the client, and fill in the QQ account and password of the other QQ account in the program.

You need to register a Sina Weibo app account. You need to register two Sina Weibo accounts, one of which is verified by the Sina Weibo app and then save the access token to set the two accounts to pay attention to each other.

After running the program, you will not only receive a command line prompt, but will also use a Sina Weibo account to send a microblog. Another Sina Weibo account will display the microblog, which contains a verification code image, use another Sina Weibo account to reply to this Weibo message. The content is the text on the Verification Code image.

On QQ, you can see that the QQ is online. If you enter logout, QQ will go offline and the program will exit automatically.

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.