iQQ 学习笔记1说明 :登录、验证码、收消息
QQ最基本的功能是登录、收消息。iQQ基于WebQQ协议,WebQQ的登录过程中还会要求用户输入验证码,因此调用iQQ的最基本的功能就是登录、验证码、收消息。
iQQ 学习笔记1程序 :登录、验证码、收消息
其中的username常量的值应替换为QQ号,password常量的值应替换为该QQ号对应的QQ密码。
iQQ 学习笔记1测试 :登录、验证码、收消息
测试本程序需要注册两个QQ号,并互相加为好友。保持一个QQ使用客户端登录,另一个QQ的QQ号和密码填写在程序中。
运行程序,将出现如下提示
复制代码
查看图片,人工输入验证码,然后在QQ客户端向这个QQ发消息,例如输入Hello!,程序将显示如下所示内容
复制代码
这就说明程序运行成功了
QQ最基本的功能是登录、收消息。iQQ基于WebQQ协议,WebQQ的登录过程中还会要求用户输入验证码,因此调用iQQ的最基本的功能就是登录、验证码、收消息。
iQQ 学习笔记1程序 :登录、验证码、收消息
其中的username常量的值应替换为QQ号,password常量的值应替换为该QQ号对应的QQ密码。
- package test;
- /**
- * 用于手工输入验证码
- */
- import java.io.BufferedReader;
- /**
- * 用于创建验证码文件
- */
- import java.io.File;
- /**
- * ImageIO.write和InputStreamReader.readLine会抛出IOException异常
- */
- import java.io.IOException;
- /**
- * 用于获取人工输入的验证码
- */
- import java.io.InputStreamReader;
- /**
- * 用于生成验证码图片
- */
- import javax.imageio.ImageIO;
- /**
- * QQ客户端接口
- */
- import iqq.im.QQClient;
- /**
- * QQActionFuture.waitFinalEvent 会抛出该异常
- */
- import iqq.im.QQException;
- /**
- * QQ通知事件监听器
- */
- import iqq.im.QQNotifyListener;
- /**
- * WebQQ客户端的实现
- */
- import iqq.im.WebQQClient;
- /**
- * QQActorDispathcer接口的实现,用于WebQQClient
- */
- import iqq.im.actor.ThreadActorDispatcher;
- /**
- * QQ消息
- */
- import iqq.im.bean.QQMsg;
- /**
- * QQClient.login中的参数
- */
- import iqq.im.bean.QQStatus;
- /**
- * QQActionFuture.waitFinalEvent的返回值类型
- */
- import iqq.im.event.QQActionEvent;
- /**
- * QQClient的远程函数的返回值类型
- */
- import iqq.im.event.QQActionFuture;
- /**
- * QQNotifyListener.onNotifyEvent的参数
- */
- import iqq.im.event.QQNotifyEvent;
- /**
- * QQNotifyEvent.getTarget的值类型
- */
- import iqq.im.event.QQNotifyEventArgs;
- /**
- *
- */
- import iqq.im.event.QQActionEvent.Type;
- public class test_1 {
- /**
- * 声明一个QQ号常量
- */
- private static final String username = "**********";
- /**
- * 声明一个密码常量
- */
- private static final String password = "**********";
- /**
- * 声明一个QQ客户端接口QQClient的变量
- */
- private static QQClient client;
- public static void main(String[] args) {
- /**
- * 创建一个WebQQ客户端对象
- */
- client = new WebQQClient(username, password, new QQNotifyListener() {
- /**
- * 声明QQ通知事件的监听函数
- */
- @Override
- public void onNotifyEvent(QQNotifyEvent event) {
- /**
- * 判断通知事件是否是收到聊天消息
- */
- if (event.getType() == QQNotifyEvent.Type.CHAT_MSG) {
- /**
- * 判断事件中的对象是否是QQ消息
- */
- if (event.getTarget() instanceof QQMsg) {
- /**
- * 获取QQ消息对象
- */
- QQMsg msg = (QQMsg) event.getTarget();
- try {
- /**
- * 获取QQ消息内容
- */
- System.out.println(msg.packContentList());
- } catch (QQException e) {
- e.printStackTrace();
- }
- }
- } else if (event.getType() == QQNotifyEvent.Type.CAPACHA_VERIFY) {
- /**
- * 判断事件中的对象是否是图片验证码
- */
- if (event.getTarget() instanceof QQNotifyEventArgs.ImageVerify) {
- /**
- * 获取图片验证码对象
- */
- QQNotifyEventArgs.ImageVerify verify = (QQNotifyEventArgs.ImageVerify) event.getTarget();
- try {
- /**
- * 把图片验证码保存在本地
- */
- ImageIO.write(verify.image, "png", new File("verify.png"));
- /**
- * 显示服务器端给出的验证码原因
- */
- System.out.println(verify.reason);
- /**
- * 在控制台显示请用户人工识别验证码
- */
- System.out.print("请输入在项目根目录下verify.png图片里面的验证码:");
- /**
- * 读取用户输入的验证码
- */
- String code = new BufferedReader(new InputStreamReader(System.in)).readLine();
- /**
- * 提交验证码
- */
- client.submitVerify(code, event);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- } else {
- /**
- * 显示未处理的QQ通知事件
- */
- System.out.println("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) {
- /**
- * 开始抓消息,此后才有可能收到 CHAT_MSG 事件
- */
- client.beginPollMsg();
- }
- } catch (QQException e) {
- e.printStackTrace();
- }
- }
- }
iQQ 学习笔记1测试 :登录、验证码、收消息
测试本程序需要注册两个QQ号,并互相加为好友。保持一个QQ使用客户端登录,另一个QQ的QQ号和密码填写在程序中。
运行程序,将出现如下提示
- 为了保证您账号的安全,请输入验证码中字符继续登录。
- 请输入在项目根目录下verify.png图片里面的验证码:
- [["font",{"style":[0,0,0],"color":"000000","name":"宋体","size":9}],"Hello!"]