/********************************************************************
*V1.0 2010-12-5 问题:获取好友列表只能获取120个好友
*每次向服务器发送命名时,如果返回了&RES=20,说明没有正确登录
*然后更改is_RightLogin的值为false,因此每次引用QQ类函数返回值的时候都要先判断
*is_RightLogin的值是否为true,否则就得考虑是不是要重新登录下QQ了
********************************************************************/
using System;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace QQ_DOMO
{
class QQ
{
public string num; //构造函数的QQ号码
private string pwd; //构造函数的QQ密码
public string[] online_Face={""}; //在线的头像号码
public string[] online_Station={""}; //在线的状态
public string[] online_Number={""}; //在线的号码
public string[] online_NameK={""}; //在线的昵称
private WebClient _client = new WebClient(); //用来给服务器发送消息的
private string postValues; //发送给服务器的字符串
private byte[] byteArray; //把要发送的字符串变成字符数组
private byte[] pageData; //接受服务器返回的字符数组
private string s; //把返回的字符数组变成字符串
public string[] MT; //储存信息类型
public string[] UN; //储存信息来源号码
public string[] MG; //储存信息内容
public bool is_RightLogin;
/// <summary>
/// QQ类的构造函数
/// </summary>
/// <param name="QQ_Num">QQ号码</param>
/// <param name="QQ_Pwd">QQ密码</param>
public QQ (string QQ_Num, string QQ_Pwd)
{
this.num = QQ_Num;
this.pwd = QQ_Pwd;
}
/// <summary>
/// 登陆QQ
/// </summary>
/// <returns>登陆成功就返回True</returns>
public bool QQ_Login()
{
postValues = "VER=1.1&CMD=Login&SEQ=" + DateTime.Now.Ticks.ToString().Substring(7, 7)
+ "&UIN=" + num + "&PS=" + MD5(pwd) + " &M5=1&LC=9326B87B234E7235";
byteArray = System.Text.Encoding.UTF8.GetBytes(postValues);
//向服务器POST数据
UploadData();
if (Encoding.UTF8.GetString(pageData).Contains("RES=0&RS=0"))
{
is_RightLogin = true;
return true;
}
else
return false;
}
public static string MD5(string toCryString)
{
MD5CryptoServiceProvider hashmd5; //using System.Security.Cryptography安全.密码系统
hashmd5 = new MD5CryptoServiceProvider();
//asp是小写,把所有字符变小写
return BitConverter.ToString(hashmd5.ComputeHash(Encoding.UTF8.GetBytes(toCryString))).Replace("-", "").ToLower();
}
/// <summary>
/// 获取QQ好友列表
/// </summary>
/// <returns>返回一个字符串数组,数组最后一个元素是空格</returns>
public string[] QQ_List()
{
postValues = "VER=1.1&CMD=List&SEQ=" + DateTime.Now.Ticks.ToString().Substring(7, 7) + "&UIN=" + num + "&TN=160&UN=0";
byteArray = System.Text.Encoding.UTF8.GetBytes(postValues);
//向服务器POST数据
UploadData();
s = Encoding.UTF8.GetString(pageData);
if (!s.Contains("&RES=0"))
is_RightLogin = false;
string s2 = s.Remove(0, s.IndexOf("&UN=")+4);
string[] QQ_Friend_List = s2.Split(',');
return QQ_Friend_List;
}
/// <summary>
/// 更新QQ类中目前在线online_四个字符串数组的值
/// </summary>
public void QQ_Query_Stat()
{
postValues = "VER=1.1&CMD=Query_Stat&SEQ=" + DateTime.Now.Ticks.ToString().Substring(7, 7) + "&UIN=" + num + "&TN=50&UN=0";
byteArray = System.Text.Encoding.UTF8.GetBytes(postValues);
//向服务器POST数据
UploadData();
s = Encoding.UTF8.GetString(pageData);
if (!s.Contains("&RES=0"))
is_RightLogin = false;
StringBuilder sb = new StringBuilder(s);
sb.Remove(s.IndexOf("&FN="), s.Length - s.IndexOf("&FN="));
sb.Remove(0,s.IndexOf("&FC=")+4);
online_Face = sb.ToString().Split(',');
sb = new StringBuilder(s);
sb.Remove(s.IndexOf("&UN="), s.Length - s.IndexOf("&UN="));
sb.Remove(0, s.IndexOf("&ST=") + 4);
online_Station = sb.ToString().Split(',');
sb = new StringBuilder(s);
sb.Remove(s.IndexOf("&NK="), s.Length - s.IndexOf("&NK="));
sb.Remove(0, s.IndexOf("&UN=") + 4);
online_Number = sb.ToString().Split(',');
string ss = s.Remove(0, s.IndexOf("&NK=") + 4);
online_NameK = ss.Split(',');
}
/// <summary>
/// 输入一个QQ号,查询这个QQ号的信息
/// </summary>
/// <param name="search_num">输入一个QQ号,查询该QQ信息</param>
/// <returns>字符串数组(联系地址,用户年龄,用户邮箱,头像,个人网站,职业,邮箱,联系电话,简介,省份,真实姓名,毕业院校,性别,QQ号,昵称)</returns>
public string[] QQ_GetInfo(string search_num)
{
postValues = "VER=1.1&CMD=GetInfo&SEQ=" + DateTime.Now.Ticks.ToString().Substring(7, 7) + "&UIN=" + num + "&LV=2&UN=" + search_num;
byteArray = System.Text.Encoding.UTF8.GetBytes(postValues);
//向服务器POST数据
UploadData();
s = Encoding.UTF8.GetString(pageData);
if (!s.Contains("&RES=0"))
is_RightLogin = false;
MatchCollection matches = Regex.Matches(s, "&([^=][^=])=([^&]*)");
List<string> Info = new List<string>();
for (int i = 0; i < matches.Count; i++)
Info.Add(matches[i].Groups[2].ToString());
Info.RemoveAt(6); //去除LV=多少, 这表示查询方式,默然就是普通查询
if (Info[12].ToString() == "0")
Info[12] = "男";
else
Info[12] = "女";
string[] Inf = Info.ToArray();
return Inf;
}
/// <summary>
/// 添加好友功能
/// </summary>
/// <param name="fir_num">输入一个QQ号,请求加为好友</param>
/// <returns>0表示已经加为好友,1表示需要验证请求,2表示拒绝</returns>
public string AddToList(string fir_num)
{
//WebClient _client = new WebClient();
postValues = "VER=1.1&CMD=AddToList&SEQ=" + DateTime.Now.Ticks.ToString().Substring(7, 7) + "&UIN=" + num + "&UN=" + fir_num;
byteArray = System.Text.Encoding.UTF8.GetBytes(postValues);
//向服务器POST数据
UploadData();
s = Encoding.UTF8.GetString(pageData);
if (!s.Contains("&RES=0"))
is_RightLogin = false;
MatchCollection matchs = Regex.Matches(s,"&CD=(.)");
return matchs[0].Groups[1].ToString();
}
/// <summary>
/// 回应加为好友的响应
/// </summary>
/// <param name="fri_Num">请求的QQ号码</param>
/// <param name="agree_Type">0表示通过验证,1表示拒绝对方,2表示请求加对方为好友</param>
public void Ack_AddToList(string fri_Num,string agree_Type)
{
//WebClient _client = new WebClient();
postValues = "VER=1.1&CMD=Ack_AddToList&SEQ=" + DateTime.Now.Ticks.ToString().Substring(7, 7) + "&UIN=" + num + "&UN=" + fri_Num + "&CD="+agree_Type+"&RS=";
byteArray = System.Text.Encoding.UTF8.GetBytes(postValues);
//向服务器POST数据
UploadData();
s=Encoding.UTF8.GetString(pageData);
if (!s.Contains("&RES=0"))
is_RightLogin = false;
}
/// <summary>
/// 删除好友,成功返回True
/// </summary>
/// <param name="del_num">输入一个QQ号,删除这个QQ好友</param>
/// <returns></returns>
public bool DelFromList(string del_num)
{
postValues = "VER=1.1&CMD=DelFromList&SEQ=" + DateTime.Now.Ticks.ToString().Substring(7, 7) + "&UIN=" + num + "&UN=" + del_num;
byteArray = System.Text.Encoding.UTF8.GetBytes(postValues);
//向服务器POST数据
UploadData();
s = Encoding.UTF8.GetString(pageData);
if (s.Contains("&RES=0"))
return true;
else
return false;
}
/// <summary>
/// 改变QQ当前状态(在线,离线,忙碌)
/// </summary>
/// <param name="Stat">输入10在线,20离线,30忙碌</param>
/// <returns></returns>
public bool Change_Stat(string stat)
{
postValues = "VER=1.1&CMD=Change_Stat&SEQ=" + DateTime.Now.Ticks.ToString().Substring(7, 7) + "&UIN=" + num + "&ST=" + stat;
byteArray = System.Text.Encoding.UTF8.GetBytes(postValues);
//向服务器POST数据
UploadData();
s = Encoding.UTF8.GetString(pageData);
if(s.Contains("&RES=0"))
return true;
else
return false;
}
/// <summary>
/// 向一个QQ号码发送消息
/// </summary>
/// <param name="msgTo">输入一个QQ号,向他发送消息</param>
/// <param name="msg">输入消息内容</param>
/// <returns>成功返回True</returns>
public bool QQ_SendMsg(string msgTo, string msg)
{
postValues = "VER=1.2&CMD=CLTMSG&SEQ=" + DateTime.Now.Ticks.ToString().Substring(7, 7) + "&UIN=" + num + "&UN=" + msgTo + "&MG=" + msg;
byteArray = System.Text.Encoding.UTF8.GetBytes(postValues);
//向服务器POST数据
UploadData();
s = Encoding.UTF8.GetString(pageData);
if (s.Contains("&RES=20"))
{
is_RightLogin = false;
return false;
}
if (s.Contains("&RES=0"))
return true;
else
return false;
}
//待处理
public void GetMsgEx()
{
postValues = "VER=1.1&CMD=GetMsgEx&SEQ=" + DateTime.Now.Ticks.ToString().Substring(7, 7) + "&UIN=" + num;
byteArray = System.Text.Encoding.UTF8.GetBytes(postValues);
//向服务器POST数据
UploadData();
s = Encoding.UTF8.GetString(pageData);
if (s.Contains("\r"))
s = s.Replace("\r", "\n");
if (s.Contains("&RES=0"))
{
is_RightLogin = true;
MatchCollection matches = Regex.Matches(s, "&MN=([^&]*)");
if (matches[0].Groups[1].ToString() != "0") //判断返回的信息数量是否为0条
{
matches = Regex.Matches(s, "&MT=([^&]*)&UN=([^&]*)&MG=([^&]*)");
MT = matches[0].Groups[1].ToString().Split(','); //信息类型
UN = matches[0].Groups[2].ToString().Split(','); //信息来源号码
s = s.Remove(0, s.IndexOf("&MG=") + 4);
MG = s.Split(','); //信息内容
//将消息内容进行转码
for(int i = 0; i<MG.Length-1;i++)
{
MG[i] = MG[i].Replace("%25", "%");
MG[i] = MG[i].Replace("%26", "&");
MG[i] = MG[i].Replace("%2c", ",");
}
}
else
{
MT = null;
UN = null;
MG = null;
is_RightLogin = false;
}
}
}
/// <summary>
/// QQ退出登陆,并改变is_RightLogin为False
/// </summary>
public void QQ_Logout()
{
postValues = "VER=1.1&CMD=Logout&SEQ=" + DateTime.Now.Ticks.ToString().Substring(7, 7) + "&UIN=" + num;
byteArray = System.Text.Encoding.UTF8.GetBytes(postValues);
//向服务器POST数据
UploadData();
s = Encoding.UTF8.GetString(pageData);
if (s.Contains("&RES=0"))
is_RightLogin = false;
}
private void UploadData()
{
try
{
pageData = _client.UploadData("http://tqq.tencent.com:8000/", "POST", byteArray);
}
catch { }
}
}
}
--------------------------------------------
using System;
using System.Net.Mail;
using System.Text.RegularExpressions;
namespace QQ_DOMO
{
class Program
{
private static QQ myQQ;
//输出居中的文本
private static void ConsoleWriteCenter(string s, ConsoleColor color)
{
Console.ForegroundColor = color;
for (int i = 0; i < Console.BufferWidth / 2 - s.Length / 2; i++)
Console.Write(" ");
Console.Write(s);
Console.WriteLine();
Console.ResetColor();
}
//输入一些文本
private static void ConsoleWrite(string s)
{
Console.Write(s);
}
//画出=号组成的边界
private static void WriteBorder()
{
Console.ForegroundColor = ConsoleColor.Blue;
for (int i = 0; i < Console.BufferWidth; i++)
Console.Write("=");
Console.ResetColor();
}
//输入一行文本
private static void ConsoleWriteLine(string s)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(s);
Console.ResetColor();
}
//选择Y或者N
private static bool ConsoleSelectYN()
{
Console.ForegroundColor = ConsoleColor.Yellow;
string s = Console.ReadLine();
if (s.ToUpper() == "Y")
{
Console.ResetColor();
return true;
}
if (s.ToUpper() == "N")
{
Console.ResetColor();
return false;
}
else
{
Console.WriteLine("No Opinion Matches: " + s);
Console.ResetColor();
return false;
}
}
//判断是否是一个QQ号码
private static bool regexQQ(string s)
{
Match match = Regex.Match(s, "[1-9][0-9]{4,}");
if (string.IsNullOrEmpty(match.Value.ToString()))
{
Console.WriteLine("你个家伙, " + s + "根本不是QQ号码!");
return false;
}
else
return true;
}
//控制台读一行文字
private static string ConsoleReadLine()
{
Console.ForegroundColor = ConsoleColor.Yellow;
string s = Console.ReadLine();
Console.ResetColor();
return s;
}
//控制台输出有哪些功能
private static int writeFunc()
{
Console.WriteLine("欢迎选择以下功能[1/2/3/4/5]:");
ConsoleWriteLine("1. 查询在线好友" + "\t\t" + "2. 查询某QQ号信息");
ConsoleWriteLine("3. 给某QQ好友发送消息" + "\t" + "4. 查看消息");
ConsoleWriteLine("5. 退出QQ");
ConsoleWrite("请您选择一个功能:\t");
string s = ConsoleReadLine();
int selectNum;
try
{
selectNum = Convert.ToInt32(s);
if (0 < selectNum && selectNum < 6)
{
WriteBorder();
return selectNum;
}
else
{
Console.WriteLine("No Opinion Matches:\t" + selectNum);
WriteBorder();
return 0;
}
}
catch
{
Console.WriteLine("您输入了错误的选项!");
WriteBorder();
return 0;
}
}
//登陆
private static void myQQLogin()
{
ConsoleWriteLine("正在登陆...需要大概10秒钟时间...");
if (myQQ.QQ_Login() == true)
ConsoleWriteLine("登陆成功...");
else
ConsoleWriteLine(@"登陆失败...您还没有去http://wp.qq.com/开通QQ在线状态功能");
}
//获取在线好友
private static void myQQOnLine()
{
ConsoleWriteLine("正在查询在线好友列表, 请稍等...");
myQQ.QQ_Query_Stat();
ConsoleWriteLine("状态" + "\t" + "号码" + "\t\t" + "昵称");
for (int i = 0; i < myQQ.online_NameK.Length; i++)
{
string station;
if (myQQ.online_Station[i] == "10")
station = "在线";
if (myQQ.online_Station[i] == "20")
station = "离开";
if (myQQ.online_Station[i] == "30")
station = "忙碌";
else
station = "在线";
if (!string.IsNullOrEmpty(myQQ.online_Number[i]))
ConsoleWriteLine(station + "\t" + myQQ.online_Number[i] + "\t" + myQQ.online_NameK[i]);
}
WriteBorder();
}
//查询某人信息
private static void myQQQuery(string num)
{
string[] information = myQQ.QQ_GetInfo(num);
ConsoleWriteLine("联系地址:" + information[0] + "\t" + "用户年龄:" + information[1] + "\t" + "用户邮箱:" + information[2]);
ConsoleWriteLine("个人网站:" + information[4] + "\t" + "职业:" + information[5] + "\t" + "邮编:" + information[6]);
ConsoleWriteLine("联系电话:" + information[7] + "\t" + "简介:" + information[8]);
ConsoleWriteLine("省份:" + information[9] + "\t" + "真实姓名:" + information[10] + "\t" + "毕业院校:" + information[11]);
ConsoleWriteLine("性别:" + information[12] + "\t\t" + "QQ号:" + information[13] + "\t" + "昵称:" + information[14]);
WriteBorder();
}
//发送消息
private static void myQQSend(string num, string msg)
{
if (myQQ.QQ_SendMsg(num, msg) == true)
{
ConsoleWriteLine("发送成功...");
}
else
{
ConsoleWriteLine("发送失败...");
}
}
//接受消息
private static void myQQGet()
{
myQQ.GetMsgEx();
if (myQQ.MT != null)
{
for (int i = 0; i < myQQ.MG.Length; i++)
{
if (myQQ.MT[i] == "9")
ConsoleWriteLine(myQQ.UN[i] + "说:\t" + myQQ.MG[i]);
}
}
else
{
ConsoleWriteLine("您现在还没有收到信息...");
}
WriteBorder();
}
static void Main(string[] args)
{
#region test
WriteBorder();
ConsoleWriteCenter("MY NAME IS CHEN HUA I MADE THIS JUST FOR FUN", ConsoleColor.Green);
ConsoleWriteCenter("THIS IDEA'S COPYRIGHT IS RESERVED BY CHEN HUA", ConsoleColor.Green);
ConsoleWriteCenter(@"PLEASE ATTENTION ME AT www.cnblogs.com/technology", ConsoleColor.Cyan);
ConsoleWriteCenter("DO NOT DO ANYTHING ILLEGAL WITH THIS DEMO OR IDEA", ConsoleColor.Red);
WriteBorder();
bool agreeOrNot;
do
{
ConsoleWrite("同意作者上面的内容? [Y/N]" + "\t");
agreeOrNot = ConsoleSelectYN();
} while (agreeOrNot == false);
WriteBorder();
Begin:
//输入QQ号码
string sQQNum;
do
{
ConsoleWrite("赶紧输入你的QQ号码:" + "\t");
sQQNum = ConsoleReadLine().Trim();
agreeOrNot = regexQQ(sQQNum);
} while (agreeOrNot == false);
string sQQPwd;
do
{
ConsoleWrite("再输入" + sQQNum + "的密码:" + "\t");
sQQPwd = ConsoleReadLine().Trim();
} while (agreeOrNot == false);
WriteBorder();
//开始一些功能
myQQ = new QQ(sQQNum, sQQPwd); //构造QQ
myQQLogin();
WriteBorder();
#endregion
//进入死循环执行功能
do
{
//获取宣传哪一项功能
int selectNum;
do
{
selectNum = writeFunc(); //写出QQDemo目前支持的一些功能
if (selectNum == 0)
agreeOrNot = false;
else
agreeOrNot = true;
if (selectNum == 1)
myQQOnLine();
if (selectNum == 2)
{
ConsoleWrite("输入你要查询哪个QQ号码:" + "\t");
string num = ConsoleReadLine();
if (regexQQ(num))
{
myQQQuery(num);
}
}
if (selectNum == 3)
{
ConsoleWrite("请输入收消息的QQ好友:\t");
string num = ConsoleReadLine();
string msg;
if (regexQQ(num) == true)
{
ConsoleWrite("请输入消息内容:\t\t");
msg = ConsoleReadLine();
myQQ.QQ_SendMsg(num, msg);
WriteBorder();
}
}
if (selectNum == 4)
{
myQQGet();
}
if (selectNum == 5)
{
myQQ.QQ_Logout();
ConsoleWriteLine("成功退出...");
WriteBorder();
goto Begin;
}
} while (agreeOrNot == false);
} while (true);
}
}
}