C#实现QQ空间登录

源码在这里:点击打开链接

这是我自己完成的QQ空间登录代码,只做到登录为止,后面没有了。

下面有两个版本,一个是windows应用,一个是控制台应用,具体如下。

代码主要是包括通过Httpget(httpwebrequest方法)登录的,还包括了QQ密码的最新加密算法2014年)

截图如下:

1.windows应用版

C#实现QQ空间登录_第1张图片


C#实现QQ空间登录_第2张图片


2.控制台应用版本

C#实现QQ空间登录_第3张图片

展示下核心代码吧:以下代码是控制台版的主要代码。完整源码链接已经提供了,要的赶紧去下,才五分,不贵。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Drawing;
using System.Security.Cryptography;
using System.Net;

namespace testWebRequest
{
    public class Program
    {
        public static String qq = "XXXXXX";    //自己填
        private static String password = "XXXXXX";  //自己填

        static void Main(String[] args)
        {
            QQLogin(qq, password);
        }

        public static void QQLogin(String QQ, String password)
        {

            String pass = "";
            String responseData = "";
            //获取验证信息
            //验证信息格式为:ptui_checkVC('0','!MIW','\x00\x00\x00\x00\x9a\x65\x0f\xd7') 
            //其中分为三部分,第一个值0或1判断是否需要图片验证码
            //                          第二个值是默认验证码,若不需要图片验证码,就用此验证码来提交
            //                          第三部分是所使用的QQ号码的16进制形式
            String forCheckUrl = "http://check.ptlogin2.qq.com/check?uin=" + QQ + "&appid=549000912&r=0.10299430438317358";
            RetData outcomeFromCheck = new RetData();
            CookieContainer cookieNull = new CookieContainer();
            HttpHelper helper = new HttpHelper();
            String receiveFromCheck;
            outcomeFromCheck = helper.GetHtml(forCheckUrl, cookieNull);
            receiveFromCheck = outcomeFromCheck.str;
            //Console.WriteLine(receiveFromCheck); //打印获取的网页内容

            //将验证码信息的三部分存入数组
            int checkCodePosition = receiveFromCheck.IndexOf("(") + 1;
            String checkCode = receiveFromCheck.Substring(checkCodePosition, receiveFromCheck.LastIndexOf(")") - checkCodePosition);
            String[] checkNum = checkCode.Replace("'", "").Split(',');  //验证码数组

            if ("1".Equals(checkNum[0])) //判断是否需要图片验证码
            {
                String forImageUrl = "http://captcha.qq.com/getimage?aid=549000912&uin=" + QQ + "&cap_cd=" + checkNum[1];
                Stream receiveStream = helper.GetStream(forImageUrl, outcomeFromCheck.cookie);
                //将获取的图片验证码存入电脑
                System.Drawing.Image.FromStream(receiveStream).Save(@"d:/code.jpg");
                //Image img = Image.FromStream(receiveStream);
                Console.Write("请输入验证码:");
                responseData = Console.ReadLine(); //将读取到的图片验证码输入到responseData中,由于是控制台应用
            }
            else //若不需图片验证码,验证码就等于checkNum[1]
            {
                responseData = checkNum[1];
            }

            //获取加密密码
            PasswordHelper helper2 = new PasswordHelper();
            pass = helper2.GetPassword(QQ, password, responseData);
            //Console.WriteLine(pass);

            /** *********************** 登录 *************************** */
            String forLoginUrl = "http://ptlogin2.qq.com/login?u=" + QQ + "&verifycode=" + responseData + "&p=" + pass + "&aid=549000912&u1=http%3A%2F%2Fqzs.qq.com%2Fqzone%2Fv5%2Floginsucc.html%3Fpara%3Dizone&h=1&t=1&g=1&from_ui=1&ptlang=2052&action=3-21-1397619935139";
            RetData outcomeFromLogin = new RetData();
            outcomeFromLogin = helper.GetHtml(forLoginUrl, outcomeFromCheck.cookie);
            String revFromLogin = outcomeFromLogin.str;
            Console.WriteLine(revFromLogin); //打印登录后返回信息
            //至此登录成功

            //返回个人空间主页
            RetData outcomeFromIndexPage = new RetData();
            outcomeFromIndexPage = helper.GetHtml("http://user.qzone.qq.com/"+qq, outcomeFromLogin.cookie);
            //Console.WriteLine(outcomeFromIndexPage[0].str); //显示主页内容

            //将主页存到txt里
            StreamWriter swrite = new StreamWriter("E:/11.txt", false, Encoding.UTF8);
            swrite.WriteLine(outcomeFromIndexPage.str);
            swrite.Close();
            swrite.Dispose();

            //遍历CookieContainer

            //GetCookieContent showCookies = new GetCookieContent();
            //showCookies.ShowAllCookies(outcomeFromLogin[0].cookie);
        }
    }


    public struct RetData //网页请求返回对象
    {
        public string str;
        public CookieContainer cookie;
    }

}

完整源码地址: http://download.csdn.net/detail/lai444132348/7266331

你可能感兴趣的:(C#编程)