[CSDN-luomingui]QQ空间留言C#
- 发表于
- VC
定时每天自动给固定QQ空间发布不同的留言。
功能列表:
1:定时器。
2:登陆QQ空间。
3:给固定QQ空间留言(随机读取预先设定的文本txt)。
现在的问题:
1: 登陆成功了。
2:发布留言的时候提示没有登陆。
全部代码如下:
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 |
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(); } } } |
原文连接:[CSDN-luomingui]QQ空间留言C#
所有媒体,可在保留署名、
原文连接
的情况下转载,若非则不得使用我方内容。