dotNet QQ大法[原创]
昨天下了个qqlite03,研究了一番不算太好用。不过还是从中挖出了一些宝贝。
qqlite03是用JS去做的,在我这边表现不太好(有可能是防火墙的原因)。我把它用dotNet写了个类,表现还算可以。不过在asp.net 2.0里就不能用了,只能在1.1中用,不知其解。拿去来与大家分享。
qqlite03是用JS去做的,在我这边表现不太好(有可能是防火墙的原因)。我把它用dotNet写了个类,表现还算可以。不过在asp.net 2.0里就不能用了,只能在1.1中用,不知其解。拿去来与大家分享。
using System;
using System.IO;
using System.Text;
using System.Net;
using System.Data;
using System.Collections;
namespace Goodspeed.IM
{
/**//// <summary>
/// Summary description for QQ
/// </summary>
public class QQ
{
- 属性 -#region - 属性 -
private string _uid = string.Empty;
private string _password = string.Empty;
public string UID
{
get { return _uid; }
}
#endregion
- 构造 -#region - 构造 -
public QQ(){}
public QQ(string UID, string PS)
{
PS = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(PS, "md5");
_password = PS;
_uid = UID;
}
#endregion
- 方法 -#region - 方法 -
//登录
public bool Login()
{
string sData = "VER=1.0&CMD=Login&SEQ=" + GetTickCount() + "&UIN="+_uid+"&PS=" + _password + "&M5=1&LC=9326B87B234E7235";
string r =SentMsg(sData);
return (r.IndexOf("密码错误") == -1);
}
//在线好友列表
public DataTable getOnlineList()
{
string sData = "VER=1.0&CMD=Query_Stat&SEQ=" + GetTickCount() + "&UIN="+_uid+"&TN=300&UN=0";
string r = SentMsg(sData);
//string r = "VER=1.0&SEQ=4688&CMD=QUERY_STAT&UIN=4480911&RES=0&SN=17&FN=1&UN=364121,503894,963947,4874923,5130883,11273626,11845218,17197891,22566157,22882044,26819204,32746072,37245281,59535722,86755989,253466760,258259468&FC=141,108,141,30,168,96,237,3,36,57,255,141,195,57,168,33,180&ST=30,10,30,10,10,10,30,10,10,10,10,10,10,10,10,10,10&NK=%20%20%20奎狼,山鹰,阿鱼,名山工作室,%20情结,billy,天骄,%20%20%20%20jordan,快乐宝贝,阿不,十二月的某天,飞羊习习鸟,green,**~田心,殷若离,冰狐%0a,%20%20移动通道";
string[] my = r.Split('&');
Hashtable ht = getParameter(my);
string[] QQnum = (ht["UN"].ToString()).Split(',');
string[] QQface = (ht["FC"].ToString()).Split(',');
string[] QQnick = (ht["NK"].ToString()).Split(',');
DataTable objTable = new DataTable();
DataColumn myDataColumn;
DataRow myDataRow;
myDataColumn = new DataColumn();
myDataColumn.DataType = System.Type.GetType("System.String");
myDataColumn.ColumnName = "num";
objTable.Columns.Add(myDataColumn);
myDataColumn = new DataColumn();
myDataColumn.DataType = System.Type.GetType("System.String");
myDataColumn.ColumnName = "face";
objTable.Columns.Add(myDataColumn);
myDataColumn = new DataColumn();
myDataColumn.DataType = System.Type.GetType("System.String");
myDataColumn.ColumnName = "nick";
objTable.Columns.Add(myDataColumn);
for (int i = 0; i < QQnum.Length; i++)
{
myDataRow = objTable.NewRow();
myDataRow["num"] = QQnum[i];
myDataRow["face"] = QQface[i];
myDataRow["nick"] = QQnick[i].Replace("%20", " ");
objTable.Rows.Add(myDataRow);
}
return objTable;
}
//好友列表
public DataTable getList()
{
string sData = "VER=1.0&CMD=List&SEQ="+GetTickCount()+"&UIN="+_uid+"&TN=300&UN=0";
string r = SentMsg(sData);
//VER=1.0&SEQ=6984&CMD=LIST&UIN=4480911&RES=0&SN=77&FN=1&UN=209433,276938,286066,329085,342780,364121
string[] my = r.Split('&');
Hashtable ht = getParameter(my);
string[] QQnum = (ht["UN"].ToString()).Split(',');
DataTable objTable = new DataTable();
DataColumn myDataColumn;
DataRow myDataRow;
myDataColumn = new DataColumn();
myDataColumn.DataType = System.Type.GetType("System.String");
myDataColumn.ColumnName = "num";
objTable.Columns.Add(myDataColumn);
for (int i = 0; i < QQnum.Length; i++)
{
myDataRow = objTable.NewRow();
myDataRow["num"] = QQnum[i];
objTable.Rows.Add(myDataRow);
}
return objTable;
}
//好友信息
public QQFriendInfo getInfo(int UN)
{
return getInfo(UN.ToString());
}
public QQFriendInfo getInfo(string UN)
{
string sData = "VER=1.0&CMD=GetInfo&SEQ=" + GetTickCount() + "&UIN=" + _uid + "&UN=" + UN;
string r = SentMsg(sData);
string[] my = r.Split('&');
Hashtable ht = getParameter(my);
QQFriendInfo qq = new QQFriendInfo(ht);
return qq;
}
//接收消息
public string GetMsg()
{
string sData = "VER=1.0&CMD=GetMsg&SEQ="+GetTickCount()+"&UIN="+_uid;
//VER=1.0&SEQ=6984&CMD=GETMSG&UIN=4480911&RES=0&MT=9&UN=63027896&MG=好,你来了~~
string r = SentMsg(sData);
string[] my = r.Split('&');
Hashtable ht = getParameter(my);
string UN = ht["UN"].ToString();
if (UN == "0") return "";
QQFriendInfo qq = getInfo(UN);
return qq.NickName + "(" + UN + "):" + ht["MG"].ToString();
}
//发送消息
public void SendMessage(int UN, string msg)
{
SendMessage(UN.ToString(), msg);
}
public void SendMessage(string UN, string msg)
{
string sData = "VER=1.0&CMD=CLTMSG&SEQ=1091071257750&UIN=4480911&UN=" + UN + "&MG=" + msg;
SentMsg(sData);
}
//登出
public void Logout()
{
string sData = "VER=1.0&CMD=Logout&SEQ=" + GetTickCount() + "&UIN=" + _uid;
SentMsg(sData);
}
#endregion
私有方法#region 私有方法
//方法返回一个整数值,这个整数代表了从 1970 年 1 月 1 日开始计算到 Date 对象中的时间之间的毫秒数
private string GetTickCount()
{
DateTime o = new DateTime(1970, 1, 1);
TimeSpan ts = DateTime.Now - o;
return ts.TotalMilliseconds.ToString("0");
}
private string SentMsg(string sData)
{
string g_qqServer = "http://127.0.0.1:8000/";
// string g_qqServer ="http://tqq.tencent.com:8000/";
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(sData);
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(g_qqServer);
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream = myRequest.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
HttpWebResponse res = (HttpWebResponse)myRequest.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream());
string sHtml = sr.ReadToEnd();
sr.Close();
res.Close();
return sHtml;
}
private Hashtable getParameter(string[] my)
{
Hashtable ht = new Hashtable();
foreach (string si in my)
{
string[] sArray = si.Split('=');
ht.Add(sArray[0], sArray[1]);
}
return ht;
}
#endregion
}
public class QQFriendInfo
{
- 属性 -#region - 属性 -
private string _qqnum = string.Empty;
private string _nickname = string.Empty;
private string _face = string.Empty;
private string _age = string.Empty;
private string _sex = string.Empty;
private string _province = string.Empty;
private string _email = string.Empty;
private string _address = string.Empty;
private string _postcode = string.Empty;
private string _phone = string.Empty;
private string _job = string.Empty;
private string _homepage = string.Empty;
private string _remark = string.Empty;
public string QQNum{get { return QQNum; }}
public string NickName { get { return _nickname; } }
public string Face { get { return _face; } }
public string Age { get { return _age; } }
public string Sex { get { return _sex; } }
public string Province { get { return _province; } }
public string Email { get { return _email; } }
public string Address { get { return _address; } }
public string Postcode { get { return _postcode; } }
public string Phone { get { return _phone; } }
public string Job { get { return _job; } }
public string HomePage { get { return _homepage; } }
public string Rremark { get { return _remark; } }
public QQFriendInfo(Hashtable ht)
{
/**//*
UIN=4480911
FC=156
AG=0
SX=0
PV=湖北省
EM=goodspeed@21cn.com
AD=湖北省武汉市63612264邮政信箱
PC=430063
PH=
RN=什么都没有
PR=VIM:30028181149%0d%0aUC:279163%0d%0a搜Q:good2speed@sohu.com
JB=失业中
SC=-
HP=http://www.cnblogs.com/goodspeed
*/
_qqnum = ht["UIN"].ToString();
_nickname = ht["NK"].ToString().Replace("%20", " ");
_address = ht["AD"].ToString();
_age = ht["AG"].ToString();
_email = ht["EM"].ToString();
_face = ht["FC"].ToString();
_homepage = ht["HP"].ToString();
_job = ht["JB"].ToString();
_phone = ht["PH"].ToString();
_postcode = ht["PC"].ToString();
_province = ht["PV"].ToString();
_remark = ht["PR"].ToString();
_sex = (ht["SX"].ToString() == "0") ? "男" : "女";
}
#endregion
}
}
using System.IO;
using System.Text;
using System.Net;
using System.Data;
using System.Collections;
namespace Goodspeed.IM
{
/**//// <summary>
/// Summary description for QQ
/// </summary>
public class QQ
{
- 属性 -#region - 属性 -
private string _uid = string.Empty;
private string _password = string.Empty;
public string UID
{
get { return _uid; }
}
#endregion
- 构造 -#region - 构造 -
public QQ(){}
public QQ(string UID, string PS)
{
PS = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(PS, "md5");
_password = PS;
_uid = UID;
}
#endregion
- 方法 -#region - 方法 -
//登录
public bool Login()
{
string sData = "VER=1.0&CMD=Login&SEQ=" + GetTickCount() + "&UIN="+_uid+"&PS=" + _password + "&M5=1&LC=9326B87B234E7235";
string r =SentMsg(sData);
return (r.IndexOf("密码错误") == -1);
}
//在线好友列表
public DataTable getOnlineList()
{
string sData = "VER=1.0&CMD=Query_Stat&SEQ=" + GetTickCount() + "&UIN="+_uid+"&TN=300&UN=0";
string r = SentMsg(sData);
//string r = "VER=1.0&SEQ=4688&CMD=QUERY_STAT&UIN=4480911&RES=0&SN=17&FN=1&UN=364121,503894,963947,4874923,5130883,11273626,11845218,17197891,22566157,22882044,26819204,32746072,37245281,59535722,86755989,253466760,258259468&FC=141,108,141,30,168,96,237,3,36,57,255,141,195,57,168,33,180&ST=30,10,30,10,10,10,30,10,10,10,10,10,10,10,10,10,10&NK=%20%20%20奎狼,山鹰,阿鱼,名山工作室,%20情结,billy,天骄,%20%20%20%20jordan,快乐宝贝,阿不,十二月的某天,飞羊习习鸟,green,**~田心,殷若离,冰狐%0a,%20%20移动通道";
string[] my = r.Split('&');
Hashtable ht = getParameter(my);
string[] QQnum = (ht["UN"].ToString()).Split(',');
string[] QQface = (ht["FC"].ToString()).Split(',');
string[] QQnick = (ht["NK"].ToString()).Split(',');
DataTable objTable = new DataTable();
DataColumn myDataColumn;
DataRow myDataRow;
myDataColumn = new DataColumn();
myDataColumn.DataType = System.Type.GetType("System.String");
myDataColumn.ColumnName = "num";
objTable.Columns.Add(myDataColumn);
myDataColumn = new DataColumn();
myDataColumn.DataType = System.Type.GetType("System.String");
myDataColumn.ColumnName = "face";
objTable.Columns.Add(myDataColumn);
myDataColumn = new DataColumn();
myDataColumn.DataType = System.Type.GetType("System.String");
myDataColumn.ColumnName = "nick";
objTable.Columns.Add(myDataColumn);
for (int i = 0; i < QQnum.Length; i++)
{
myDataRow = objTable.NewRow();
myDataRow["num"] = QQnum[i];
myDataRow["face"] = QQface[i];
myDataRow["nick"] = QQnick[i].Replace("%20", " ");
objTable.Rows.Add(myDataRow);
}
return objTable;
}
//好友列表
public DataTable getList()
{
string sData = "VER=1.0&CMD=List&SEQ="+GetTickCount()+"&UIN="+_uid+"&TN=300&UN=0";
string r = SentMsg(sData);
//VER=1.0&SEQ=6984&CMD=LIST&UIN=4480911&RES=0&SN=77&FN=1&UN=209433,276938,286066,329085,342780,364121
string[] my = r.Split('&');
Hashtable ht = getParameter(my);
string[] QQnum = (ht["UN"].ToString()).Split(',');
DataTable objTable = new DataTable();
DataColumn myDataColumn;
DataRow myDataRow;
myDataColumn = new DataColumn();
myDataColumn.DataType = System.Type.GetType("System.String");
myDataColumn.ColumnName = "num";
objTable.Columns.Add(myDataColumn);
for (int i = 0; i < QQnum.Length; i++)
{
myDataRow = objTable.NewRow();
myDataRow["num"] = QQnum[i];
objTable.Rows.Add(myDataRow);
}
return objTable;
}
//好友信息
public QQFriendInfo getInfo(int UN)
{
return getInfo(UN.ToString());
}
public QQFriendInfo getInfo(string UN)
{
string sData = "VER=1.0&CMD=GetInfo&SEQ=" + GetTickCount() + "&UIN=" + _uid + "&UN=" + UN;
string r = SentMsg(sData);
string[] my = r.Split('&');
Hashtable ht = getParameter(my);
QQFriendInfo qq = new QQFriendInfo(ht);
return qq;
}
//接收消息
public string GetMsg()
{
string sData = "VER=1.0&CMD=GetMsg&SEQ="+GetTickCount()+"&UIN="+_uid;
//VER=1.0&SEQ=6984&CMD=GETMSG&UIN=4480911&RES=0&MT=9&UN=63027896&MG=好,你来了~~
string r = SentMsg(sData);
string[] my = r.Split('&');
Hashtable ht = getParameter(my);
string UN = ht["UN"].ToString();
if (UN == "0") return "";
QQFriendInfo qq = getInfo(UN);
return qq.NickName + "(" + UN + "):" + ht["MG"].ToString();
}
//发送消息
public void SendMessage(int UN, string msg)
{
SendMessage(UN.ToString(), msg);
}
public void SendMessage(string UN, string msg)
{
string sData = "VER=1.0&CMD=CLTMSG&SEQ=1091071257750&UIN=4480911&UN=" + UN + "&MG=" + msg;
SentMsg(sData);
}
//登出
public void Logout()
{
string sData = "VER=1.0&CMD=Logout&SEQ=" + GetTickCount() + "&UIN=" + _uid;
SentMsg(sData);
}
#endregion
私有方法#region 私有方法
//方法返回一个整数值,这个整数代表了从 1970 年 1 月 1 日开始计算到 Date 对象中的时间之间的毫秒数
private string GetTickCount()
{
DateTime o = new DateTime(1970, 1, 1);
TimeSpan ts = DateTime.Now - o;
return ts.TotalMilliseconds.ToString("0");
}
private string SentMsg(string sData)
{
string g_qqServer = "http://127.0.0.1:8000/";
// string g_qqServer ="http://tqq.tencent.com:8000/";
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(sData);
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(g_qqServer);
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream = myRequest.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
HttpWebResponse res = (HttpWebResponse)myRequest.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream());
string sHtml = sr.ReadToEnd();
sr.Close();
res.Close();
return sHtml;
}
private Hashtable getParameter(string[] my)
{
Hashtable ht = new Hashtable();
foreach (string si in my)
{
string[] sArray = si.Split('=');
ht.Add(sArray[0], sArray[1]);
}
return ht;
}
#endregion
}
public class QQFriendInfo
{
- 属性 -#region - 属性 -
private string _qqnum = string.Empty;
private string _nickname = string.Empty;
private string _face = string.Empty;
private string _age = string.Empty;
private string _sex = string.Empty;
private string _province = string.Empty;
private string _email = string.Empty;
private string _address = string.Empty;
private string _postcode = string.Empty;
private string _phone = string.Empty;
private string _job = string.Empty;
private string _homepage = string.Empty;
private string _remark = string.Empty;
public string QQNum{get { return QQNum; }}
public string NickName { get { return _nickname; } }
public string Face { get { return _face; } }
public string Age { get { return _age; } }
public string Sex { get { return _sex; } }
public string Province { get { return _province; } }
public string Email { get { return _email; } }
public string Address { get { return _address; } }
public string Postcode { get { return _postcode; } }
public string Phone { get { return _phone; } }
public string Job { get { return _job; } }
public string HomePage { get { return _homepage; } }
public string Rremark { get { return _remark; } }
public QQFriendInfo(Hashtable ht)
{
/**//*
UIN=4480911
FC=156
AG=0
SX=0
PV=湖北省
EM=goodspeed@21cn.com
AD=湖北省武汉市63612264邮政信箱
PC=430063
PH=
RN=什么都没有
PR=VIM:30028181149%0d%0aUC:279163%0d%0a搜Q:good2speed@sohu.com
JB=失业中
SC=-
HP=http://www.cnblogs.com/goodspeed
*/
_qqnum = ht["UIN"].ToString();
_nickname = ht["NK"].ToString().Replace("%20", " ");
_address = ht["AD"].ToString();
_age = ht["AG"].ToString();
_email = ht["EM"].ToString();
_face = ht["FC"].ToString();
_homepage = ht["HP"].ToString();
_job = ht["JB"].ToString();
_phone = ht["PH"].ToString();
_postcode = ht["PC"].ToString();
_province = ht["PV"].ToString();
_remark = ht["PR"].ToString();
_sex = (ht["SX"].ToString() == "0") ? "男" : "女";
}
#endregion
}
}
posted on 2004-07-28 16:05 Goodspeed 阅读(2685) 评论(7) 编辑 收藏 举报