iCLAuto,实现自己写的一个简单论坛自动定时评论功能,处女贴,欢迎留言批评指正!...

这是我花了近一天的时间,鼓捣出来的一个卖萌自动评论器,界面很简单,如图:

 

 

代码如下:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Text;
  7 using System.Windows.Forms;
  8 using System.Text.RegularExpressions;
  9 
 10 namespace iCLauto
 11 {
 12     public partial class Form1 : Form
 13     {
 14         
 15         HttpHelper helper;        //HttpHelper class to post data and get html string,and urlEncode
 16         int count1024 = 1024;    //time to count, 1024 is default seconds.
 17         int successCount = 0;    //10 comments can be posted a day. 
 18         bool isExit = false;    //the flag to judge exit.
 19         string webside = "http://xx.xx.xx/";//这里不便透露是哪个网站
 20         string contentType = @"application/x-www-form-urlencoded";    //contentType used in HttpHelper class.
 21         public Form1()
 22         {
 23             InitializeComponent();
 24             helper = new HttpHelper();
 25         }
 26 
 27         private void button1_Click(object sender, EventArgs e)
 28         {
 29             if (UserLogin())
 30             {
 31                 MessageBox.Show("主人,您的密码正确哦。");
 32             }
 33             else
 34             {
 35                 MessageBox.Show("555555,你记错伦家密码了啦~");
 36             }
 37         }
 38         
 39         //Username and Password judge. return true if correct.
 40         private bool UserLogin()
 41         {
 42             string username = HttpHelper.UrlEncode(txtUsername.Text.Trim(), "gb2312");
 43             string password = txtPasswd.Text.Trim();
 44             StringBuilder builder = new StringBuilder();
 45             builder.AppendFormat("pwuser={0}&pwpwd={1}&", username, password);
 46             builder.Append("jumpurl=index.php&step=2&cktime=31536000");
 47             helper.ClearCookie();
 48             string html = helper.PostAndGetHtml(webside + "login.php", builder.ToString(),
 49                 contentType, null, false, Encoding.Default);
 50             if (html.Contains("您已經順利登錄") || html.Contains("請不要重複登錄"))
 51             {
 52                 return true;
 53             }
 54             else
 55             {
 56                 return false;
 57             }
 58         }
 59         private void btnStart_Click(object sender, EventArgs e)
 60         {
 61             if (btnStart.Text=="开始工作")
 62             {
 63                 if (UserLogin())
 64                 {
 65                     lblNotify.Text = "用户密码正确,主淫我开始工作啦~";
 66                 }
 67                 btnVerify.Enabled = false;
 68                 btnStart.Text = "停止工作";
 69                 timer1.Enabled = true;
 70                 label1.Visible = false;
 71                 label2.Visible = false;
 72                 label5.Visible = false;
 73                 txtPasswd.Visible = false;
 74                 txtUsername.Visible = false;
 75                 txtContent.Visible = false;
 76                 btnVerify.Visible = false;
 77                 btnStart.Location = new Point(0, 0);
 78             } 
 79             else if(btnStart.Text=="停止工作")
 80             {
 81                 lblNotify.Text = "主人,不要停啊,伦家还要嘛~";
 82                 btnVerify.Enabled = true;
 83                 btnStart.Text = "开始工作";
 84                 timer1.Enabled = false;
 85                 label1.Visible = true;
 86                 label2.Visible = true;
 87                 label5.Visible = true;
 88                 txtPasswd.Visible = true;
 89                 txtUsername.Visible = true;
 90                 txtContent.Visible = true;
 91                 btnVerify.Visible = true;
 92                 btnStart.Location = new Point(190, 201);
 93             }
 94             else
 95             {
 96                 isExit = true;
 97                 this.Close();
 98             }
 99 
100         }
101         private bool AutoMsg()
102         {
103             if (helper.CC.Count==0)
104             {
105                 if (!UserLogin())
106                 {
107                     lblNotify.Text = "主人,用户或密码不正确哦~";
108                     btnStart.PerformClick();
109                     return false;
110                 }
111             }
112             
113             //get the newest article number.
114             string html = helper.GetAndGetHtml(webside + "thread0806.php?fid=7", contentType, null, false, Encoding.Default);
115             string matchString = @"(?<=htm_data/7/1211/)(.+?)(?=.html)";
116             Regex reg = new Regex(matchString);
117             MatchCollection mc = reg.Matches(html);
118             string msgNum = mc[3].ToString();
119             
120             //match the article title
121             matchString = "(?<=htm_data/7/1211/" + msgNum +".html\" " +"target=\"_blank\" id=\"\">" + @")(.+?)(?=</a></h3>)";
122             reg = new Regex(matchString);
123             MatchCollection mc2 = reg.Matches(html);
124             
125             //Build the postData string
126             StringBuilder builder = new StringBuilder();
127             string title=HttpHelper.UrlEncode(mc2[0].ToString(),"gb2312");
128             
129             //avoid the case of highlight title
130             if (title.Contains("</font>"))
131             {
132                 reg = new Regex("(?<=<font color=.+?>)(.+?)(?=</font>)",RegexOptions.RightToLeft);
133                 title = reg.Matches(title)[0].ToString();
134             }
135             
136             //build the formData to post
137             string content = HttpHelper.UrlEncode(txtContent.Text.Trim(), "gb2312");
138             builder.Append("atc_money=0&atc_rvrc=0&atc_title=Re%3A");
139             builder.Append(title);
140             builder.Append("&atc_usesign=1&atc_convert=1&atc_autourl=1&atc_content=");
141             builder.Append(content);
142             builder.Append("&step=2&action=reply&fid=7&tid=");
143             builder.Append(msgNum);
144             builder.Append("&editor=0&atc_attachment=none&verify=verify");
145             html = helper.PostAndGetHtml(webside + "post.php?", builder.ToString(), contentType, null, false, Encoding.Default);
146             if (html.Contains("灌水預防機制"))
147             {
148                 return false;
149             }
150             return true;
151         }
152 
153         private void timer1_Tick(object sender, EventArgs e)
154         {
155             lblNextTime.Text = count1024.ToString()+"";
156             if (0 == count1024--)
157             {
158             
159                 //add 1 to successCount
160                 if (AutoMsg())
161                     lblCount.Text = (++successCount).ToString()+"";
162                 if (successCount==10)
163                 {
164                     lblNotify.Text = "今日任务已完成哦,主人,爱你!";
165                     timer1.Enabled = false;
166                     btnStart.Text = "完成退出!";
167                     return;
168                 }
169                 //A random number to mixture Diss
170                 Random r = new Random();
171                 count1024=r.Next(1030, 1050);
172             }
173         }
174 
175         private void Form1_Load(object sender, EventArgs e)
176         {
177             lblNotify.Text = "我是一只可爱的小兔子,主淫我爱你哦。";
178             lblNextTime.Text = "1024秒";
179             lblCount.Text = "0次";
180             this.MaximizeBox = false;
181         }
182 
183         private void 在ToolStripMenuItem_Click(object sender, EventArgs e)
184         {
185             this.Show();
186         }
187 
188         private void notifyIcon1_DoubleClick(object sender, EventArgs e)
189         {
190             this.Show();
191             this.Activate();
192         }
193 
194         private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
195         {
196                 isExit = true;
197                 this.Close();   
198         }
199 
200         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
201         {
202             if (!isExit)
203             {
204                 this.Hide();
205                 e.Cancel = true;
206             }
207         }
208     }
209 }

再发一个HttpHelper,做网页相关的应用很有用的:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Text;
  4 using System.Net;
  5 using System.IO;
  6 using System.Drawing;
  7 using System.Text.RegularExpressions;
  8 
  9 namespace iCLauto
 10 {
 11     class HttpHelper
 12     {
 13         private CookieContainer cc;
 14 
 15         public CookieContainer CC
 16         {
 17             get
 18             {
 19                 return cc;
 20             }
 21             set
 22             {
 23                 this.cc = value;
 24             }
 25         }
 26 
 27         public HttpHelper()
 28         {
 29             this.cc = new CookieContainer();
 30         }
 31 
 32         public HttpHelper(CookieContainer cc)
 33         {
 34             this.cc = cc;
 35         }
 36 
 37         ///<summary>
 38         /// 使用post方式访问目标网页,返回stream二进制流
 39         ///</summary>
 40         public Stream PostAndGetStream(string targetURL, string formData, string contentType, string referer, bool allowAutoRedirect)
 41         {
 42             //数据编码
 43             ASCIIEncoding encoding = new ASCIIEncoding();
 44             byte[] data = encoding.GetBytes(formData);
 45 
 46             //请求目标网页
 47             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(targetURL);
 48             request.CookieContainer = cc;
 49             request.Method = "POST";    //使用post方式发送数据
 50             request.ContentType = contentType;
 51             request.Referer = referer;
 52             request.AllowAutoRedirect = allowAutoRedirect;
 53             request.ContentLength = data.Length;
 54             request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1; .NET CLR 2.0.1124)";
 55 
 56             //模拟一个UserAgent
 57             Stream newStream = request.GetRequestStream();
 58             newStream.Write(data, 0, data.Length);
 59             newStream.Close();
 60 
 61             //获取网页响应结果
 62             HttpWebResponse response = (HttpWebResponse)request.GetResponse();
 63             cc.Add(response.Cookies);
 64             Stream stream = response.GetResponseStream();
 65             return stream;
 66         }
 67 
 68         public string PostByteAndGetHtml(string targetURL, byte[] data, string contentType, string referer, bool allowAutoRedirect)
 69         {
 70             //数据编码
 71 
 72             //请求目标网页
 73             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(targetURL);
 74             request.CookieContainer = cc;
 75             request.Method = "POST";    //使用post方式发送数据
 76             request.ContentType = contentType;
 77             request.Referer = referer;
 78             request.AllowAutoRedirect = allowAutoRedirect;
 79             request.ContentLength = data.Length;
 80             request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1; .NET CLR 2.0.1124)";
 81 
 82             //模拟一个UserAgent
 83             Stream newStream = request.GetRequestStream();
 84             newStream.Write(data, 0, data.Length);
 85             newStream.Close();
 86 
 87             //获取网页响应结果
 88             HttpWebResponse response = (HttpWebResponse)request.GetResponse();
 89             cc.Add(response.Cookies);
 90             Stream stream = response.GetResponseStream();
 91             string html = new StreamReader(stream, Encoding.Default).ReadToEnd();
 92             return html;
 93         }
 94         ///<summary>
 95         /// 使用post方式访问目标网页,返回字节数组
 96         ///</summary>
 97         public byte[] PostAndGetByte(string targetURL, string formData, string contentType, string referer, bool allowAutoRedirect)
 98         {
 99             Stream stream = PostAndGetStream(targetURL, formData, contentType, referer, allowAutoRedirect);
100             byte[] bytes = new byte[stream.Length];
101             stream.Read(bytes, 0, bytes.Length);
102             stream.Seek(0, SeekOrigin.Begin);
103             return bytes;
104         }
105 
106         ///<summary>
107         /// 使用post方式访问目标网页,返回图片
108         ///</summary>
109         public Image PostAndGetBitmap(string targetURL, string formData, string contentType, string referer, bool allowAutoRedirect)
110         {
111             Stream stream = PostAndGetStream(targetURL, formData, contentType, referer, allowAutoRedirect);
112             Image image = Image.FromStream(stream);
113             return image;
114         }
115 
116         ///<summary>
117         /// 使用post方式访问目标网页,返回文件
118         ///</summary>
119         public void PostAndGetBitmap(string targetURL, string formData, string contentType, string referer, bool allowAutoRedirect,string fileName)
120         {
121             byte[] bytes = PostAndGetByte(targetURL, formData, contentType, referer, allowAutoRedirect);
122             FileStream fs = new FileStream(fileName, FileMode.Create);
123             BinaryWriter bw = new BinaryWriter(fs);
124             bw.Write(bytes);
125             bw.Close();
126             fs.Close();
127         }
128 
129         ///<summary>
130         /// 使用post方式访问目标网页,返回html页面
131         ///</summary>
132         public string PostAndGetHtml(string targetURL, string formData, string contentType, string referer, bool allowAutoRedirect, Encoding encoding)
133         {
134             Stream stream = PostAndGetStream(targetURL, formData, contentType, referer, allowAutoRedirect);
135             string html = new StreamReader(stream, encoding).ReadToEnd();
136             return html;
137         }
138 
139 
140         ///<summary>
141         /// 使用get方式访问目标网页,返回stream二进制流
142         ///</summary>
143         public Stream GetAndGetStream(string targetURL, string contentType, string referer, bool allowAutoRedirect)
144         {
145             //请求目标网页
146             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(targetURL);
147             request.CookieContainer = cc;
148             request.Method = "GET";    //使用get方式发送数据
149             request.ContentType = contentType;
150             request.Referer = referer;
151             request.AllowAutoRedirect = allowAutoRedirect;
152             request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1; .NET CLR 2.0.1124)";
153 
154 
155             //获取网页响应结果
156             HttpWebResponse response = (HttpWebResponse)request.GetResponse();
157             cc.Add(response.Cookies);
158             Stream stream = response.GetResponseStream();
159             return stream;
160         }
161 
162         ///<summary>
163         /// 使用get方式访问目标网页,返回字节数组
164         ///</summary>
165         public byte[] GetAndGetByte(string targetURL, string contentType, string referer, bool allowAutoRedirect)
166         {
167             Stream stream = GetAndGetStream(targetURL, contentType, referer, allowAutoRedirect);
168             byte[] bytes = new byte[stream.Length];
169             stream.Read(bytes, 0, bytes.Length);
170             stream.Seek(0, SeekOrigin.Begin);
171             return bytes;
172         }
173 
174         ///<summary>
175         /// 使用get方式访问目标网页,返回图片
176         ///</summary>
177         public Image GetAndGetBitmap(string targetURL, string contentType, string referer, bool allowAutoRedirect)
178         {
179             Stream stream = GetAndGetStream(targetURL, contentType, referer, true);
180             Image image = Image.FromStream(stream);
181             return image;
182         }
183 
184         ///<summary>
185         /// 使用get方式访问目标网页,返回文件
186         ///</summary>
187         public void GetAndGetFile(string targetURL, string contentType, string referer, bool allowAutoRedirect, string fileName)
188         {
189             byte[] bytes = GetAndGetByte(targetURL, contentType, referer, allowAutoRedirect);
190             FileStream fs = new FileStream(fileName, FileMode.Create);
191             BinaryWriter bw = new BinaryWriter(fs);
192             bw.Write(bytes);
193             bw.Close();
194             fs.Close();
195         }
196 
197         ///<summary>
198         /// 使用get方式访问目标网页,返回html页面
199         ///</summary>
200         public string GetAndGetHtml(string targetURL, string contentType, string referer, bool allowAutoRedirect, Encoding encoding)
201         {
202             try
203             {
204                 Stream stream = GetAndGetStream(targetURL, contentType, referer, allowAutoRedirect);
205                 string html = new StreamReader(stream, encoding).ReadToEnd();
206                 return html;
207             }
208             catch (System.Exception ex)
209             {
210                 return "null"; 
211             }
212  
213         }
214         public static string UrlEncode(string str,string strCode)
215         {
216             StringBuilder sb = new StringBuilder();
217             byte[] byStr = System.Text.Encoding.GetEncoding(strCode).GetBytes(str); //默认是System.Text.Encoding.Default.GetBytes(str)
218             for (int i = 0; i < byStr.Length; i++)
219             {
220                 sb.Append(@"%" + Convert.ToString(byStr[i], 16));
221             }
222             return (sb.ToString().ToUpper());
223         }
224         public bool ClearCookie()
225         {
226             this.cc=new CookieContainer();
227             return true;
228         }
229     }
230 }

 

转载于:https://www.cnblogs.com/GuidoWhicky/archive/2012/11/22/2783436.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值