C# 登录QQ网站并获取QQ相关信息
2012-09-12 17:56
351 查看
本文以登录http://id.qq.com为例,获取用户等级、Q龄等相关信息。 本文界面使用WPF制作 技术涵盖:C#、wpf、HttpWebRequest、HttpWebResponse (.net framework 4.5,当然你也可以使用4.0。不知道为什么使用3.5无法得到正确的结果,代码一模一样,还望高人指点!) |
1、获取独立登录框地址 |
![](http://img.my.csdn.net/uploads/201209/12/1347443436_5070.gif)
2)Open link in new tab
![](http://img.my.csdn.net/uploads/201209/12/1347443496_4554.gif)
到这里我们已经得到独立登陆框的地址了,方便调试和拦截请求。
成果
登录框地址:
http://ui.ptlogin2.qq.com/cgi-bin/login?appid=1006102&hide_title_bar=1&begin_time=1347444392246
&css=http%3A%2F%2Fimgcache.qq.com%2Fptcss%2Fr1%2Fjt%2F1006102%2Fpt_login.css
&f_url=loginerroralert&no_verifyimg=1&qlogin_jumpname=jump&hide_close_icon=1&s_url=http://id.qq.com/index.html
URL中有很多控制样式的参数,可简化为如下地址:
http://ui.ptlogin2.qq.com/cgi-bin/login?appid=1006102&s_url=http://id.qq.com/index.html
2、获取登录时请求的地址 |
1)打开上一步得到的登录地址,并开启浏览器的开发人员工具,用以拦截请求。
2)随便输入一个号码、密码、验证码后点击登录,得到如下结果:
![](http://img.my.csdn.net/uploads/201209/13/1347527119_6960.gif)
我们可以看到一共发送了3个请求:
请求名称 | 返回值 | 请求地址 | 描述 |
check | 不通过: ptui_checkVC('1', 'a89853e618880c1b93041bda88f5fb673c8eb5555a8850fd', '\x00\x00\x00\x00\x01\x53\x15\x8e'); 通过: ptui_checkVC('0','!5A3', '\x00\x00\x00\x00\x05\xef\x0f\xba'); | http://check.ptlogin2.qq.com/check? uin=22222222&appid=1006102&r=0.10299430438317358 | 当输入QQ号码后文本框失去焦点时发送该请求,用于检查此次登录是否需要验证码。 |
getimage | 验证码图片 | http://captcha.qq.com/getimage?aid=1006102&r=0.06791123608127236&uin=22222222 | 如果上一步的check方法不通过则会自动发送这个请求获取验证码。 |
login | ptuiCB('3','0','','0', '您输入的帐号或密码不正确,请重新输入。', '22222222'); | http://ptlogin2.qq.com/login?u=22222222&p=712F9A90BABE147B7A3A4427DF48698C&verifycode=gqfj& aid=1006102&u1=http%3A%2F%2Fid.qq.com%2Findex.html& h=1&ptredirect=1&ptlang=2052&from_ui=1&dumy=& fp=loginerroralert&action=8-29-82478035&mibao_css=&t=1&g=1 | 点击登录按钮时发送该请求进行登录操作。 |
var M = C.p.value; var I = hexchar2bin(md5(M)); var H = md5(I + pt.uin); var G = md5(H + C.verifycode.value.toUpperCase());
加密方法翻译为C#后如下:
public class PasswordHelper { /// <summary> /// 根据QQ号码和验证码加密密码 /// </summary> /// <param name="qqNum">QQ号码</param> /// <param name="password">QQ密码</param> /// <param name="verifycode">验证码</param> /// <returns>密码密文</returns> public static string GetPassword(string qqNum, string password, string verifycode) { //uin为QQ号码转换为16位的16进制 int qq; int.TryParse(qqNum, out qq); qqNum = qq.ToString("x"); qqNum = qqNum.PadLeft(16, '0'); String P = hexchar2bin(md5(password)); String U = md5(P + hexchar2bin(qqNum)).ToUpper(); String V = md5(U + verifycode.ToUpper()).ToUpper(); return V; } public static string md5(string input) { byte[] buffer = MD5.Create().ComputeHash(Encoding.GetEncoding("ISO-8859-1").GetBytes(input)); return binl2hex(buffer); } public static string binl2hex(byte[] buffer) { StringBuilder builder = new StringBuilder(); for (int i = 0; i < buffer.Length; i++) { builder.Append(buffer[i].ToString("x2")); } return builder.ToString(); } public static string hexchar2bin(string passWord) { StringBuilder builder = new StringBuilder(); for (int i = 0; i < passWord.Length; i = i + 2) { builder.Append(Convert.ToChar(Convert.ToInt32(passWord.Substring(i, 2), 16))); } return builder.ToString(); }
3、使用WPF构建登录框 |
![](http://img.my.csdn.net/uploads/201209/13/1347529566_5433.gif)
我所使用的HttpHelper如下,很简单,大家可以自行扩展:
public class HttpHelper { private static string contentType = "application/x-www-form-urlencoded"; private static string accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-silverlight-2-b1, */*"; private static string userAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; Zune 4.7; BOIE9;ZHCN)"; public static string referer = "http://ui.ptlogin2.qq.com/cgi-bin/login?appid=1006102&s_url=http://id.qq.com/index.html"; /// <summary> /// 获取字符流 /// </summary> /// <param name="url"></param> /// <param name="cookieContainer"></param> /// <returns></returns> public static Stream GetStream(string url, CookieContainer cookieContainer) { HttpWebRequest httpWebRequest = null; HttpWebResponse httpWebResponse = null; try { httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url); httpWebRequest.CookieContainer = cookieContainer; httpWebRequest.ContentType = contentType; httpWebRequest.Referer = referer; httpWebRequest.Accept = accept; httpWebRequest.UserAgent = userAgent; httpWebRequest.Method = "GET"; httpWebRequest.ServicePoint.ConnectionLimit = int.MaxValue; httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); Stream responseStream = httpWebResponse.GetResponseStream(); return responseStream; } catch (Exception) { return null; } } /// <summary> /// 获取HTML /// </summary> /// <param name="url"></param> /// <param name="cookieContainer"></param> /// <returns></returns> public static string GetHtml(string url, CookieContainer cookieContainer) { HttpWebRequest httpWebRequest = null; HttpWebResponse httpWebResponse = null; try { httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url); httpWebRequest.CookieContainer = cookieContainer; httpWebRequest.ContentType = contentType; httpWebRequest.Referer = referer; httpWebRequest.Accept = accept; httpWebRequest.UserAgent = userAgent; httpWebRequest.Method = "GET"; httpWebRequest.ServicePoint.ConnectionLimit = int.MaxValue; httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); Stream responseStream = httpWebResponse.GetResponseStream(); StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8); string html = streamReader.ReadToEnd(); streamReader.Close(); responseStream.Close(); httpWebRequest.Abort(); httpWebResponse.Close(); return html; } catch (Exception) { return string.Empty; } } }
4、登录成功后查询我们需要的信息 |
注:ldw从登录后的cookie中获取
然后继续发送请求:
![](http://img.my.csdn.net/uploads/201209/13/1347530022_3230.gif)
自此我们的功能基本上完成了,登录成功后查询其他数据也都很方便了。
5、注意点提示: |
祝你成功!只要登录后可以直接获取QQ空间信息或WebQQ中的信息亦或是操作QQ游戏。
相关文章推荐
- 获取第三方登录(相关信息 新浪微博&微信&QQ)&&登录和分享接口功能
- [C#.Net]利用HttpRequest登录到某个网站,然后获取网站信息的程序示例
- iOS的应用中集成QQ第三方登录获取相关信息
- 正则表达式相关:C# 抓取网页类(获取网页中所有信息)
- C# 根据ip获取城市等相关信息
- 网站分类前导:获取网站标题和描述及对相关信息进行分词处理
- 利用HttpRequest登录到某个网站,然后获取网站信息的程序示例
- C#获取本机相关mac和ip信息以及服务器ip
- ManagementClass类解析和C#如何获取硬件的相关信息
- C# 根据ip获取城市等相关信息
- 友盟第三方登录获取QQ信息
- C#获取一个文件的相关信息
- c#获取本机电脑相关信息
- [asp.net]利用HttpRequest登录到某个网站,然后获取网站信息
- 利用HttpRequest登录到某个网站,然后获取网站信息的程序示例
- 分别嵌入 新浪微博、QQ、微信 做第三方授权登录 获取到头像 昵称等信息
- C# 获取音乐相关信息
- c#获取本机电脑相关信息
- c#获取本机电脑相关信息
- C#获取电脑的相关信息