[CSDN-luomingui]QQ空间留言C#

  • A+
所属分类:WooYun-Zone

1:定时每天自动给固定QQ空间发布不同的留言。
功能列表:
1:定时器。
2:登陆QQ空间。
3:给固定QQ空间留言(随机读取预先设定的文本txt)。
现在的问题:
1: 登陆成功了。
2:发布留言的时候提示没有登陆。

[CSDN-luomingui]QQ空间留言C#
全部代码如下:

namespace QQAPI
{
    public partial class Form1 : Form
    {
        public String host = "http://d.web2.qq.com";
        public String shost = "http://s.web2.qq.com";
        private Random rd = new Random();
        private String get = "get";
        private CookieContainer cookies = new CookieContainer();
        private String refer = "http://d.web2.qq.com/proxy.html?v=20110331002&callback=2";
        private String userAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 1.1.4322; .NET4.0C; .NET4.0E)";
        private String accept = "*/*";
        private String contentType = "application/x-www-form-urlencoded; charset=UTF-8";
        private String verifyCode = "";
        private String hex16 = "";
        private String skey = "";
        private String clientid = new Random().Next(100000000) + "";

        //默认需要输入验证码   
        private Boolean isVerify = false;

        public Form1()
        {
            InitializeComponent();
        }
        private void butLogin_Click(object sender, System.EventArgs e)
        {
            //登陆   
            String qq = this.txtQQNumber.Text.Trim();
          String pass = this.txtPwd.Text.Trim();
           verifyCode = this.txtcode.Text.Trim();
            String md5pass = GetPassword(hex16, pass, verifyCode);
            LoginQzone(qq, md5pass, verifyCode);
        }
        public void GetQzoneVCode()
        {
            //QQ空间_是否要验证码
            String qq = this.txtQQNumber.Text.Trim();
            String url = "http://check.ptlogin2.qq.com/check?uin=" + qq + "&appid=549000912&ptlang=2052&js_type=2&js_ver=10009&r=0." + DateTime.Now.Ticks.ToString().Substring(7, 7);
            String result = getHtml(url, get, null);
            if (result.IndexOf("!") < 0)
            {
                //需要验证图片   
                String verifyUrl = "http://captcha.qq.com/getimage?aid=1003903&uin=" + qq + "&r=" + rd.NextDouble();
                Stream img = getStream(verifyUrl, get);
                Image codeImage = Image.FromStream(img);
                this.picbox_code.Image = codeImage;
                isVerify = true;
            }
            else
            {
                this.verifyCode = result.Substring(result.IndexOf("!"), 4);
                this.hex16 = result.Substring(result.IndexOf("\\"), 32);
                this.hex16 = this.hex16.Replace("\\x", "");
            }
        }
        public void LoginQzone(string qq, string md5pass, string verifyCode)
        {
            // QQ空间_登录空间
            String loginUrl = "http://ptlogin2.qq.com/login?ptlang=2052&u=" + qq + "&p=" + md5pass + "&verifycode=" + verifyCode + "&css=http://imgcache.qq.com/ptcss/b2/sjpt/549000912/qzonelogin_ptlogin.css&mibao_css=m_qzone&aid=549000912&u1=http%3A%2F%2Fqzs.qq.com%2Fqzone%2Fv5%2Floginsucc.html%3Fpara%3Dizone&ptredirect=1&h=1&from_ui=1&dumy=&fp=loginerroralert&action=10-77-43469&g=1&t=1&dummy=&js_type=2&js_ver=10009";
            String loginResult = getHtml(loginUrl, "get", null);
            MessageBox.Show(loginResult);
            int isLogin = loginResult.IndexOf("登录成功");
            if (isLogin > 0)
            {
                butMessage.Enabled = true;
                String cks = cookies.GetCookieHeader(new Uri(loginUrl));
                this.skey = getMidStr(cks, "skey=");
            }
            else
            {
                MessageBox.Show("登陆失败");
                butMessage.Enabled = false;
            }
        }
        public bool <span style="color: #FF0000;">QzoneMessage</span>(int qqnum, string message)
        {
            try
            {
                int gtk = GetGTK(this.skey);
                string num = txtQQNumber.Text.Trim();

                WebClient _client = new WebClient();
                string host = "http://m.qzone.qq.com/cgi-bin/new/add_msgb?ref=qzone&g_tk=" + gtk + "";

                string postValues = "qzreferrer=http%3A%2F%2Fcnc.qzs.qq.com%2Fqzone%2Fmsgboard%2Fmsgbcanvas.html%23uin%3D123456%26pfid%3D2%26qz_ver%3D6%26appcanvas%3D0%26qz_style%3Dv6%2F18%26params%3D%26entertime%3D1356580709748%26canvastype%3D&content="
                    + message + "&hostUin=" + qqnum + "&uin=" + num + "&format=fs&g_tk=" + gtk + "&ref=qzone&json=1&inCharset=gbk&outCharset=gbk&iNotice=1";

                byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(postValues);
                byte[] pageData = _client.UploadData(host, "POST", byteArray);
                string s = Encoding.Default.GetString(pageData);
                this.txtMessage.Text = s;
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public string getMidStr(string cks, string flg)
        {
            string temp = "";
            string[] cksall = cks.Split(';');
            for (int i = 0; i < cksall.Length; i++)
            {
                if (cksall[i].Contains(flg))
                {
                    temp = cksall[i].Replace(flg, "").Trim();
                    break;
                }
            }
            return temp;
        }
        private void butMessage_Click(object sender, System.EventArgs e)
        {
            if (QzoneMessage(int.Parse(txtPeerQQ.Text), txtMessage.Text))
            {
                MessageBox.Show("留言成功");
            }
            else
            {
                MessageBox.Show("留言失败");
            }
        }
        private void Form1_Load(object sender, System.EventArgs e)
        {
            butMessage.Enabled = false;
        }
        public int GetGTK(string skey)
        {
            int gtk = 0;
            int hash = 5381;
            string str = skey;
            for (int i = 0, len = str.Length; i < len; ++i)
            {
                hash += (hash << 5) + str.ElementAt(i);
            }
            gtk = hash & 0x7fffffff;
            return gtk;
        }
        public string GetPassword(string qq, string password, string verifycode)
        {
            String P = hexchar2bin(md5(password));
            //String U = md5(P + hexchar2bin(qq.Replace("\\x", ""))).ToUpper();   
            String U = md5(P + hexchar2bin(qq)).ToUpper();
            String V = md5(U + verifycode.ToUpper()).ToUpper();
            return V;
        }
        private 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();
        }
        private string md5(string input)
        {
            byte[] buffer = MD5.Create().ComputeHash(Encoding.GetEncoding("ISO-8859-1").GetBytes(input));
            return binl2hex(buffer);
        }
        private string hexchar2bin(string passWord)
        {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < passWord.Length; i = i + 2)
            {
                try
                {
                    builder.Append(Convert.ToChar(Convert.ToInt32(passWord.Substring(i, 2), 16)));
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.ToString());
                }
            }
            return builder.ToString();
        }
        private String getHtml(String url, String method, String data)
        {
            Uri uri = new Uri(url);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
            request.UserAgent = this.userAgent;
            request.Accept = this.accept;
            request.ContentType = this.contentType;
            request.Method = method;
            request.Referer = this.refer;
            request.CookieContainer = this.cookies;
            if (method.Equals("post"))
            {
                byte[] byteRequest = Encoding.Default.GetBytes(data);
                Stream rs = request.GetRequestStream();
                rs.Write(byteRequest, 0, byteRequest.Length);
                rs.Close();
            }
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            cookies.Add(response.Cookies);
            Stream resultStream = response.GetResponseStream();
            StreamReader sr = new StreamReader(resultStream, Encoding.UTF8);
            string html = sr.ReadToEnd();
            sr.Close();
            resultStream.Close();
            request.Abort();
            response.Close();
            return html;
        }
        private Stream getStream(String url, String method)
        {
            Uri uri = new Uri(url);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
            request.UserAgent = this.userAgent;
            request.Accept = this.accept;
            request.ContentType = this.contentType;
            request.Method = method;
            request.Referer = this.refer;
            request.CookieContainer = this.cookies;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            foreach (Cookie cookie in response.Cookies)
            {
                cookies.Add(cookie);
            }
            Stream s = response.GetResponseStream();
            return s;
        }
        private void txtQQNumber_Click(object sender, EventArgs e)
        {
            GetQzoneVCode();
        }
    }
}

 

  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: