WPF界面-手机QQ_DEMO

参考别人研究的QQ协议做的简单的WPF版手机QQ聊天器,界面不太美观!

简单功能:

1.登陆
2.消息查看器
3.托盘消息闪烁
4.查看个人基本资料
5.聊天
6.点击闪烁图标与最近的好友聊天

截图


主要通讯核心类:(有些是参考的网上的,基本上自己都改动过)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Security.Cryptography;
using System.Text.RegularExpressions;
using System.Threading;
using My.Properties;
 
namespace My
{
 
    /// <summary>
    /// 获取腾讯QQ数据操作类
    /// </summary>
    public class ServerPostProcess
    {
        private string num = string.Empty;  //构造函数的QQ号码
        private string pwd = string.Empty; //构造函数的QQ密码
        private string md5Pwd = string.Empty;//加密后的QQ密码
 
        public string[] online_Face = { "" };       //在线的头像号码
        public string[] online_Station = { "" };    //在线的状态
        public string[] online_Number = { "" };     //在线的号码
        public string[] online_NameK = { "" };      //在线的昵称
 
        private WebClient wc1 = null;
        private WebClient wc2 = null;
        private WebClient wc3 = null;
 
        #region WebClient操作池(暂时放弃)出现_clientSends并发问题
        //private List<WebClient> _clientSends = new List<WebClient>(3);
        //// <summary>
        //// 尝试从集合里面拿到一个发送器
        //// </summary>
        //private WebClient ClientSends
        //{
        //    get {
        //        lock (_clientSends)
        //        {
        //            int tnum = GetClientSendsNoBusyNum;
        //            if (_clientSends.Count <= 5 && tnum <= 0)
        //            {
        //                WebClient addWc = new WebClient();
        //                _clientSends.Add(addWc);
        //                return addWc;
        //            }
        //            if (tnum == 0)
        //            {
        //                return null;
        //            }
        //            //拿出一个不繁忙的发送器进行处理请求
        //            foreach (WebClient wct in _clientSends)
        //            {
        //                if (wct != null && !wct.IsBusy)
        //                {
        //                    return wct;
        //                }
        //            }
        //            return null;
        //        }
        //    }
        //}
        ///// <summary>
        ///// 获取发送器不繁忙的可用数量
        ///// </summary>
        //private int GetClientSendsNoBusyNum
        //{
        //    get
        //    {
        //        int i = 0;
        //        foreach (WebClient wc in _clientSends)
        //        {
        //            if (wc != null && !wc.IsBusy)
        //            {
        //                i++;
        //            }
        //        }
        //        return i;
        //    }
        //}
        #endregion
 
        private string postStr;     //发送给服务器的字符串
        private byte[] postArray;   //把要发送的字符串变成字符数组
        private byte[] returnArray;  //接受服务器返回的字符数组
        private string returnStr;    //把返回的字符数组变成字符串
 
        public string[] MT;    //储存信息类型
        public string[] UN;    //储存信息来源号码
        public string[] MG;    //储存信息内容
 
        public bool is_RightLogin;//是否正确的登陆
 
        /// <summary>
        /// 登陆前提:必须通过验证
        /// </summary>
        public ServerPostProcess()
        {
            Users user = ManagerStaticResouce.LoginingUser;
            if (user != null)
            {
                num = user.Uid;
                pwd = user.Pwd;
            }
            else
            {
                throw new Exception("用户名称不存在!");
            }
            wc1 = new WebClient();
            wc2 = new WebClient();
            wc3 = new WebClient();
            md5Pwd = MD5(pwd);
        }
 
        /// <summary>
        /// 向服务器POST数据
        /// </summary>
        private bool UploadData(int type)
        {
            try
            {
                WebClient wc = GetClientSends(type);//尝试拿到一个发送器
                if (wc != null)//双重判断
                {
                    returnArray = wc.UploadData("http://tqq.tencent.com:8000", "POST", postArray);
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                throw new WebException(ex.Message);
            }
            return true;
        }
 
        /// <summary>
        /// 得到一个发送器
        /// </summary>
        /// <param name="type">操作类型(1 其他,2 发送消息,3 获取消息)</param>
        /// <returns></returns>
        private WebClient GetClientSends(int type)
        {
            if (wc1 != null && !wc1.IsBusy && type == 1)
            {
                return wc1;
            }
            else if (wc2 != null && !wc2.IsBusy && type == 2)
            {
                return wc2;
            }
            else if (wc3 != null && !wc3.IsBusy && type==3)
            {
                return wc3;
            }
            else
            {
                return null;
            }
        }
 
        /// <summary>
        /// 登陆QQ
        /// </summary>
        /// <returns>登陆成功就返回True</returns>
        public bool QQ_Login(ref string returnResult)
        {
            postStr = "VER=1.1&CMD=Login&SEQ=" + DateTime.Now.Ticks.ToString().Substring(7, 7)
                + "&UIN=" + num + "&PS=" + md5Pwd + " &M5=1&LC=9326B87B234E7235";
            postArray = System.Text.Encoding.UTF8.GetBytes(postStr);
            //向服务器POST数据
 
            if (!UploadData(1))
            {
                returnResult = "服务器忙!请重新点击登陆!";
                return false;
            }
 
            returnStr = Encoding.UTF8.GetString(returnArray);
            returnResult = returnStr;
            if (returnStr.Contains("RES=0&RS=0"))
            {
                is_RightLogin = true;
                return true;
            }
            else
            {
                return false;
            }
        }
 
        /// <summary>
        /// QQ退出登陆,并改变is_RightLogin为False
        /// </summary>
        public void QQ_Logout()
        {
            postStr = "VER=1.1&CMD=Logout&SEQ=" + DateTime.Now.Ticks.ToString().Substring(7, 7) + "&UIN=" + num;
            postArray = System.Text.Encoding.UTF8.GetBytes(postStr);
            //向服务器POST数据
            while (true)
            {
                if (!UploadData(1))
                {
                    continue;
                }
                break;
            }
            returnStr = Encoding.UTF8.GetString(returnArray);
            if (returnStr.Contains("&RES=0"))
            {
                is_RightLogin = false;
                return;
            }
        }
 
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="toCryString"></param>
        /// <returns></returns>
        public static string MD5(string toCryString)
        {
            //using System.Security.Cryptography安全.密码系统
            MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
            //asp是小写,把所有字符变小写
            string tmp =BitConverter.ToString(hashmd5.ComputeHash(Encoding.UTF8.GetBytes(toCryString))).Replace("-", "").ToLower();
            hashmd5.Clear();
            return tmp;
        }
 
        /// <summary>
        /// 获取QQ好友列表
        /// </summary>
        /// <returns>返回一个字符串数组,数组最后一个元素是空格</returns>
        public string[] QQ_List()
        {
            postStr = "VER=1.1&CMD=List&SEQ=" + DateTime.Now.Ticks.ToString().Substring(7, 7) + "&UIN=" + num + "&TN=160&UN=0";
            postArray = System.Text.Encoding.UTF8.GetBytes(postStr);
            //向服务器POST数据
            if (!UploadData(1))
            {
                return null;
            }
            returnStr = Encoding.UTF8.GetString(returnArray);
            if (!returnStr.Contains("&RES=0"))
                is_RightLogin = false;
            string s2 = returnStr.Remove(0, returnStr.IndexOf("&UN=") + 4);
            string[] QQ_Friend_List = s2.Split(',');
            return QQ_Friend_List;
        }
 
        /// <summary>
        /// 更新QQ类中目前在线online_四个字符串数组的值
        /// </summary>
        public void QQ_Query_Stat()
        {
            postStr = "VER=1.1&CMD=Query_Stat&SEQ=" + DateTime.Now.Ticks.ToString().Substring(7, 7) + "&UIN=" + num + "&TN=50&UN=0";
            postArray = System.Text.Encoding.UTF8.GetBytes(postStr);
            //向服务器POST数据
            if (!UploadData(1))
            {
                return;
            }
            returnStr = Encoding.UTF8.GetString(returnArray);
            if (!returnStr.Contains("&RES=0"))
                is_RightLogin = false;
            StringBuilder sb = new StringBuilder(returnStr);
            sb.Remove(returnStr.IndexOf("&FN="), returnStr.Length - returnStr.IndexOf("&FN="));
            sb.Remove(0, returnStr.IndexOf("&FC=") + 4);
            online_Face = sb.ToString().Split(',');
 
            sb = new StringBuilder(returnStr);
            sb.Remove(returnStr.IndexOf("&UN="), returnStr.Length - returnStr.IndexOf("&UN="));
            sb.Remove(0, returnStr.IndexOf("&ST=") + 4);
            online_Station = sb.ToString().Split(',');
 
            sb = new StringBuilder(returnStr);
            sb.Remove(returnStr.IndexOf("&NK="), returnStr.Length - returnStr.IndexOf("&NK="));
            sb.Remove(0, returnStr.IndexOf("&UN=") + 4);
            online_Number = sb.ToString().Split(',');
 
            string ss = returnStr.Remove(0, returnStr.IndexOf("&NK=") + 4);
            online_NameK = ss.Split(',');
        }
 
        /// <summary>
        /// 输入一个QQ号,查询这个QQ号的信息
        /// </summary>
        /// <param name="search_num">输入一个QQ号,查询该QQ信息</param>
        /// <returns>字符串数组(0联系地址,1用户年龄,2用户邮箱,3头像,4个人网站,5职业,6邮箱,7联系电话,8简介,9省份,10真实姓名,11毕业院校,12性别,13QQ号,14昵称)</returns>
        public string[] QQ_GetInfo(string search_num)
        {
            postStr = "VER=1.1&CMD=GetInfo&SEQ=" + DateTime.Now.Ticks.ToString().Substring(7, 7) + "&UIN=" + num + "&LV=2&UN=" + search_num;
            postArray = System.Text.Encoding.UTF8.GetBytes(postStr);
            //向服务器POST数据
            if (!UploadData(1))
            {
                return null;
            }
            returnStr = Encoding.UTF8.GetString(returnArray);
            if (!returnStr.Contains("&RES=0"))
                is_RightLogin = false;
            MatchCollection matches = Regex.Matches(returnStr, "&([^=][^=])=([^&]*)");
            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)
        {
            postStr = "VER=1.1&CMD=AddToList&SEQ=" + DateTime.Now.Ticks.ToString().Substring(7, 7) + "&UIN=" + num + "&UN=" + fir_num;
            postArray = System.Text.Encoding.UTF8.GetBytes(postStr);
            //向服务器POST数据
            if (!UploadData(1))
            {
                return null;
            }
            returnStr = Encoding.UTF8.GetString(returnArray);
            if (!returnStr.Contains("&RES=0"))
                is_RightLogin = false;
            MatchCollection matchs = Regex.Matches(returnStr, "&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)
        {
            postStr = "VER=1.1&CMD=Ack_AddToList&SEQ=" + DateTime.Now.Ticks.ToString().Substring(7, 7) + "&UIN=" + num + "&UN=" + fri_Num + "&CD=" + agree_Type + "&RS=";
            postArray = System.Text.Encoding.UTF8.GetBytes(postStr);
            //向服务器POST数据
            if (!UploadData(1))
            {
                return;
            }
            returnStr = Encoding.UTF8.GetString(returnArray);
            if (!returnStr.Contains("&RES=0"))
                is_RightLogin = false;
        }
 
        /// <summary>
        /// 删除好友,成功返回True
        /// </summary>
        /// <param name="del_num">输入一个QQ号,删除这个QQ好友</param>
        /// <returns></returns>
        public bool DelFromList(string del_num)
        {
            postStr = "VER=1.1&CMD=DelFromList&SEQ=" + DateTime.Now.Ticks.ToString().Substring(7, 7) + "&UIN=" + num + "&UN=" + del_num;
            postArray = System.Text.Encoding.UTF8.GetBytes(postStr);
            //向服务器POST数据
            if (!UploadData(1))
            {
                return false;
            }
            returnStr = Encoding.UTF8.GetString(returnArray);
            if (returnStr.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)
        {
            postStr = "VER=1.1&CMD=Change_Stat&SEQ=" + DateTime.Now.Ticks.ToString().Substring(7, 7) + "&UIN=" + num + "&ST=" + stat;
            postArray = System.Text.Encoding.UTF8.GetBytes(postStr);
            //向服务器POST数据
            if (!UploadData(1))
            {
                return false;
            }
            returnStr = Encoding.UTF8.GetString(returnArray);
            if (returnStr.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)
        {
            postStr = "VER=1.2&CMD=CLTMSG&SEQ=" + DateTime.Now.Ticks.ToString().Substring(7, 7) + "&UIN=" + num + "&UN=" + msgTo + "&MG=" + msg;
            postArray = System.Text.Encoding.UTF8.GetBytes(postStr);
            //向服务器POST数据
            if (!UploadData(2))
            {
                return false;
            }
 
            returnStr = Encoding.UTF8.GetString(returnArray);
            if (returnStr.Contains("&RES=20"))
            {
                is_RightLogin = false;
                return false;
            }
            if (returnStr.Contains("&RES=0"))
                return true;
            else
                return false;
        }
 
        //待处理
        public void GetMsgEx()
        {
            postStr = "VER=1.1&CMD=GetMsgEx&SEQ=" + DateTime.Now.Ticks.ToString().Substring(7, 7) + "&UIN=" + num;
            postArray = System.Text.Encoding.UTF8.GetBytes(postStr);
            //向服务器POST数据
            if (!UploadData(3))
            {
                return;
            }
            returnStr = Encoding.UTF8.GetString(returnArray);
            if (returnStr.Contains("\r"))
                returnStr = returnStr.Replace("\r", "\n");
            if (returnStr.Contains("&RES=0") && returnStr.Contains("&MN="))
            {
                is_RightLogin = true;
                MatchCollection matches = Regex.Matches(returnStr, "&MN=([^&]*)");
 
                if (matches[0].Groups[1].ToString() != "0") //判断返回的信息数量是否为0条
                {
                    matches = Regex.Matches(returnStr, "&MT=([^&]*)&UN=([^&]*)&MG=([^&]*)");
                    MT = matches[0].Groups[1].ToString().Split(',');   //信息类型
                    UN = matches[0].Groups[2].ToString().Split(',');   //信息来源号码
                    returnStr = returnStr.Remove(0, returnStr.IndexOf("&MG=") + 4);
                    MG = returnStr.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;
                }
            }
            else
            {
                MT = null;
                UN = null;
                MG = null;
                is_RightLogin = false;
            }
        }
 
         
 
 
    }
}

此程序纯属好玩,单纯的只想研究下手机QQ的协议.
注意没有使用过手机登陆过QQ的用户可能无法正常登陆,解决办法是使用真正的手机QQ登陆一次即可.
程序下载:NatureQQ
源码下载:NatureQQ_Source

posted @   JasNature  阅读(3372)  评论(4编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示