1:定时每天自动给固定QQ空间发布不同的留言。
功能列表:
1:定时器。
2:登陆QQ空间。
3:给固定QQ空间留言(随机读取预先设定的文本txt)。
现在的问题:
1: 登陆成功了。
2:发布留言的时候提示没有登陆。
全部代码如下:
1 namespace QQAPI 2 { 3 public partial class Form1 : Form 4 { 5 public String host = "http://d.web2.qq.com"; 6 public String shost = "http://s.web2.qq.com"; 7 private Random rd = new Random(); 8 private String get = "get"; 9 private CookieContainer cookies = new CookieContainer(); 10 private String refer = "http://d.web2.qq.com/proxy.html?v=20110331002&callback=2"; 11 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)"; 12 private String accept = "*/*"; 13 private String contentType = "application/x-www-form-urlencoded; charset=UTF-8"; 14 private String verifyCode = ""; 15 private String hex16 = ""; 16 private String skey = ""; 17 private String clientid = new Random().Next(100000000) + ""; 18 19 //默认需要输入验证码 20 private Boolean isVerify = false; 21 22 public Form1() 23 { 24 InitializeComponent(); 25 } 26 private void butLogin_Click(object sender, System.EventArgs e) 27 { 28 //登陆 29 String qq = this.txtQQNumber.Text.Trim(); 30 String pass = this.txtPwd.Text.Trim(); 31 verifyCode = this.txtcode.Text.Trim(); 32 String md5pass = GetPassword(hex16, pass, verifyCode); 33 LoginQzone(qq, md5pass, verifyCode); 34 } 35 public void GetQzoneVCode() 36 { 37 //QQ空间_是否要验证码 38 String qq = this.txtQQNumber.Text.Trim(); 39 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); 40 String result = getHtml(url, get, null); 41 if (result.IndexOf("!") < 0) 42 { 43 //需要验证图片 44 String verifyUrl = "http://captcha.qq.com/getimage?aid=1003903&uin=" + qq + "&r=" + rd.NextDouble(); 45 Stream img = getStream(verifyUrl, get); 46 Image codeImage = Image.FromStream(img); 47 this.picbox_code.Image = codeImage; 48 isVerify = true; 49 } 50 else 51 { 52 this.verifyCode = result.Substring(result.IndexOf("!"), 4); 53 this.hex16 = result.Substring(result.IndexOf("\\"), 32); 54 this.hex16 = this.hex16.Replace("\\x", ""); 55 } 56 } 57 public void LoginQzone(string qq, string md5pass, string verifyCode) 58 { 59 // QQ空间_登录空间 60 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"; 61 String loginResult = getHtml(loginUrl, "get", null); 62 MessageBox.Show(loginResult); 63 int isLogin = loginResult.IndexOf("登录成功"); 64 if (isLogin > 0) 65 { 66 butMessage.Enabled = true; 67 String cks = cookies.GetCookieHeader(new Uri(loginUrl)); 68 this.skey = getMidStr(cks, "skey="); 69 } 70 else 71 { 72 MessageBox.Show("登陆失败"); 73 butMessage.Enabled = false; 74 } 75 } 76 public bool <span style="color: #FF0000;">QzoneMessage</span>(int qqnum, string message) 77 { 78 try 79 { 80 int gtk = GetGTK(this.skey); 81 string num = txtQQNumber.Text.Trim(); 82 83 WebClient _client = new WebClient(); 84 string host = "http://m.qzone.qq.com/cgi-bin/new/add_msgb?ref=qzone&g_tk=" + gtk + ""; 85 86 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=" 87 + message + "&hostUin=" + qqnum + "&uin=" + num + "&format=fs&g_tk=" + gtk + "&ref=qzone&json=1&inCharset=gbk&outCharset=gbk&iNotice=1"; 88 89 byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(postValues); 90 byte[] pageData = _client.UploadData(host, "POST", byteArray); 91 string s = Encoding.Default.GetString(pageData); 92 this.txtMessage.Text = s; 93 return true; 94 } 95 catch (Exception) 96 { 97 return false; 98 } 99 } 100 public string getMidStr(string cks, string flg) 101 { 102 string temp = ""; 103 string[] cksall = cks.Split(‘;‘); 104 for (int i = 0; i < cksall.Length; i++) 105 { 106 if (cksall[i].Contains(flg)) 107 { 108 temp = cksall[i].Replace(flg, "").Trim(); 109 break; 110 } 111 } 112 return temp; 113 } 114 private void butMessage_Click(object sender, System.EventArgs e) 115 { 116 if (QzoneMessage(int.Parse(txtPeerQQ.Text), txtMessage.Text)) 117 { 118 MessageBox.Show("留言成功"); 119 } 120 else 121 { 122 MessageBox.Show("留言失败"); 123 } 124 } 125 private void Form1_Load(object sender, System.EventArgs e) 126 { 127 butMessage.Enabled = false; 128 } 129 public int GetGTK(string skey) 130 { 131 int gtk = 0; 132 int hash = 5381; 133 string str = skey; 134 for (int i = 0, len = str.Length; i < len; ++i) 135 { 136 hash += (hash << 5) + str.ElementAt(i); 137 } 138 gtk = hash & 0x7fffffff; 139 return gtk; 140 } 141 public string GetPassword(string qq, string password, string verifycode) 142 { 143 String P = hexchar2bin(md5(password)); 144 //String U = md5(P + hexchar2bin(qq.Replace("\\x", ""))).ToUpper(); 145 String U = md5(P + hexchar2bin(qq)).ToUpper(); 146 String V = md5(U + verifycode.ToUpper()).ToUpper(); 147 return V; 148 } 149 private string binl2hex(byte[] buffer) 150 { 151 StringBuilder builder = new StringBuilder(); 152 for (int i = 0; i < buffer.Length; i++) 153 { 154 builder.Append(buffer[i].ToString("x2")); 155 } 156 return builder.ToString(); 157 } 158 private string md5(string input) 159 { 160 byte[] buffer = MD5.Create().ComputeHash(Encoding.GetEncoding("ISO-8859-1").GetBytes(input)); 161 return binl2hex(buffer); 162 } 163 private string hexchar2bin(string passWord) 164 { 165 StringBuilder builder = new StringBuilder(); 166 for (int i = 0; i < passWord.Length; i = i + 2) 167 { 168 try 169 { 170 builder.Append(Convert.ToChar(Convert.ToInt32(passWord.Substring(i, 2), 16))); 171 } 172 catch (Exception e) 173 { 174 MessageBox.Show(e.ToString()); 175 } 176 } 177 return builder.ToString(); 178 } 179 private String getHtml(String url, String method, String data) 180 { 181 Uri uri = new Uri(url); 182 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); 183 request.UserAgent = this.userAgent; 184 request.Accept = this.accept; 185 request.ContentType = this.contentType; 186 request.Method = method; 187 request.Referer = this.refer; 188 request.CookieContainer = this.cookies; 189 if (method.Equals("post")) 190 { 191 byte[] byteRequest = Encoding.Default.GetBytes(data); 192 Stream rs = request.GetRequestStream(); 193 rs.Write(byteRequest, 0, byteRequest.Length); 194 rs.Close(); 195 } 196 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 197 cookies.Add(response.Cookies); 198 Stream resultStream = response.GetResponseStream(); 199 StreamReader sr = new StreamReader(resultStream, Encoding.UTF8); 200 string html = sr.ReadToEnd(); 201 sr.Close(); 202 resultStream.Close(); 203 request.Abort(); 204 response.Close(); 205 return html; 206 } 207 private Stream getStream(String url, String method) 208 { 209 Uri uri = new Uri(url); 210 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); 211 request.UserAgent = this.userAgent; 212 request.Accept = this.accept; 213 request.ContentType = this.contentType; 214 request.Method = method; 215 request.Referer = this.refer; 216 request.CookieContainer = this.cookies; 217 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 218 foreach (Cookie cookie in response.Cookies) 219 { 220 cookies.Add(cookie); 221 } 222 Stream s = response.GetResponseStream(); 223 return s; 224 } 225 private void txtQQNumber_Click(object sender, EventArgs e) 226 { 227 GetQzoneVCode(); 228 } 229 } 230 }
时间: 2024-12-29 06:58:43