前段时间想做个QQ的一个辅助工具,需要登陆QQ,从网上搜索了些资料,自己进行了实践和改造,可以成功登陆qq
如图:
1、获取登陆验证码,如下,如果获取的验证码为!开头,就可以不用获取图片验证码,否则需要获取图片验证码,地址为:},vc_type为上次获取的验证码,注意:获取这些信息时,必须返回Cookie
1 public static User getVerifyCodeImg( string qq) 2 { 3 HTTPModel hTTPModel = new HTTPModel( string.Format( " http://ptlogin2.qq.com/check?uin={0}&appid=15000101 ", qq)); 4 WebHTTPUtil.sendHTTP(hTTPModel); 5 CookieCollection cookie = hTTPModel.ReturnCookies; 6 string value = WebHTTPUtil.GetValue(hTTPModel.ReturnHtml, " ',' ", " ' "); 7 Console.WriteLine( " yzm: " + value); 8 User user = new User(); 9 if (value.Contains( " ! ")) 10 { 11 user.VerifyCode = value; 12 } 13 else 14 { 15 string imgUrl = string.Format( " http://captcha.qq.com/getimage?aid=1003903&&uin={0}&vc_type={1} ", qq, value); 16 user.VerifyCodeUrl = imgUrl; 17 } 18 user.QQNum = qq; 19 user.Cookie = cookie; 20 return user; 21 22 }
2、获取验证码图片,设置图片控件的image属性this.verifyImg.Image = WebHTTPUtil.byteArrayToImage(imgUrl, ref cookie);
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 public static Image byteArrayToImage( string url, ref CookieCollection cookie) 2 { 3 Uri requestUri = new Uri(url); 4 WebRequest webRequest = WebRequest.Create(requestUri); 5 Image result; 6 using (WebResponse response = webRequest.GetResponse()) 7 { 8 if (! string.IsNullOrEmpty(response.Headers[HttpResponseHeader.SetCookie])) 9 { 10 CookieContainer cookieContainer = new CookieContainer(); 11 cookie = new CookieCollection(); 12 cookieContainer.SetCookies(webRequest.RequestUri, response.Headers[HttpResponseHeader.SetCookie]); 13 cookie.Add(cookieContainer.GetCookies(webRequest.RequestUri)); 14 } 15 using (Stream responseStream = response.GetResponseStream()) 16 { 17 result = Image.FromStream(responseStream); 18 } 19 } 20 return result; 21 }
3.对QQ密码和验证码进行散列,使用MD5
public static string binl2hex( byte[] buffer) { StringBuilder stringBuilder = new StringBuilder(); for ( int i = 0; i < buffer.Length; i++) { stringBuilder.Append(buffer[i].ToString( " x2 ")); } return stringBuilder.ToString(); } public static string md5_3( string input) { MD5 mD = MD5.Create(); byte[] buffer = mD.ComputeHash(Encoding.Default.GetBytes(input)); buffer = mD.ComputeHash(buffer); buffer = mD.ComputeHash(buffer); return WebHTTPUtil.binl2hex(buffer); } public static string md5( string input) { byte[] buffer = MD5.Create().ComputeHash(Encoding.Default.GetBytes(input)); return WebHTTPUtil.binl2hex(buffer); }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 public static User LoginQZone( string qq, string pwd, string verifyCode, CookieCollection cookie, User u) 2 { 3 // object password = WebHTTPUtil.GetPassword(pwd, verifyCode); 4 System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider(); 5 string QQPwd = Md5(Md5_3(pwd) + verifyCode.ToUpper()); 6 string loginUrl = string.Format( " http://ptlogin2.qq.com/login?u={0}&p={1}&verifycode={2}&aid=15000101&u1=http%3A%2F%2Fimgcache.qq.com%2Fqzone%2Fv5%2Floginsucc.html%3Fpara%3Dizone&h=1&ptredirect=1&ptlang=2052&from_ui=1&dumy=&fp=loginerroralert ", qq, QQPwd, verifyCode); 7 HTTPModel hTTPModel = new HTTPModel(loginUrl); 8 hTTPModel.Encoding = Encoding.UTF8; 9 hTTPModel.PostCookies = cookie; 10 WebHTTPUtil.sendHTTP(hTTPModel); 11 User result; 12 Console.WriteLine(QQPwd + " " + verifyCode); 13 Console.WriteLine(loginUrl); 14 Console.WriteLine(hTTPModel.ReturnHtml); 15 }
当打印出如下,代表登陆成功
ptuiCB('0','0','http://imgcache.qq.com/qzone/v5/loginsucc.html?para=izone','1','登录成功!', 'heavy');第一步QQ登陆完成