using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using mshtml;
using System.Text.RegularExpressions;
using System.Net;
using System.IO;
using System.Threading;
namespace AutoXiaoNei
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string[] urlStr = new string[] { };
urlStr = macthValue(@"http://group.xiaonei.com/GetThread.do\?id=\d+&parentpage=0&curpage=0&label=&tribeId=\d+", htmlStr());
foreach (string m in urlStr)
{
LisBoxUrl.Items.Add(m.Replace("amp;", ""));
}
}
#region "发文函数"
///
/// 发文函数
///
/// 窗体ID
/// 提交按钮ID
/// 标题inputID
/// 内容inputID
/// 标题
/// 文章
/// boolean
public Boolean HtmlAdd(string formID, string submitID, string contentID, string cValue)
{
try
{
IHTMLDocument2 doc = (IHTMLDocument2)this.wB.Document.DomDocument;
//标题内容
//HTMLInputElement title = (HTMLInputElement)doc.all.item(titleID, 0);
//title.value = tValue;
//文章内容
//IHTMLTxtRange range = doc.selection.createRange() as IHTMLTxtRange;
//range.pasteHTML(cValue);
//HTMLInputElement content = (HTMLInputElement)doc.all.item(contentID, 0);
//content.value = cValue;
//HTMLTextAreaElement content = (HTMLTextAreaElement)doc.all.item(contentID, 0);
//content.value = cValue;
HTMLTextAreaElement area = (HTMLTextAreaElement)doc.all.item(contentID , 0);
area.value = cValue ;
//提交的窗体Form
HTMLInputElement input1= (HTMLInputElement)doc.all.item("submit", 0);
input1.value = "0";
HTMLInputElement input2 = (HTMLInputElement)doc.all.item("citename", 0);
input2.value = "0";
HTMLInputElement input3 = (HTMLInputElement)doc.all.item("at", 0);
input3.value = "0";
IHTMLFormElement f1 = (IHTMLFormElement)doc.all.item(formID, 0);
((IHTMLFormElement)(f1)).submit();
// 提交按钮
IHTMLElement Element = (IHTMLElement)doc.all.item(submitID, 0);
Element.click();
return true;
}
catch
{
return false;
}
finally { }
}
#endregion
private void btnSub_Click(object sender, EventArgs e)
{
string t = "";
foreach (object o in LisBoxUrl.Items)
{
string temp = o.ToString();
this.wB.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wB_DocumentCompleted);
wB.Navigate(temp);
t += temp;
}
this.textBox1.Text = t;
//Random r = new Random();
//HtmlAdd("editorForm", "editorFormBtn", "cmtbody", "严重同意!"+r.Next(10).ToString());
}
private void btnGetURL_Click(object sender, EventArgs e)
{
string[] urlStr = new string[] { };
urlStr = macthValue(@"http://group.xiaonei.com/GetThread.do\?id=\d+&parentpage=0&curpage=0&label=&tribeId=\d+", htmlStr());
foreach(string m in urlStr )
{
LisBoxUrl.Items.Add(m);
}
}
#region "读取Html信息"
///
/// GetHtml(string urlStr)
///
/// url地址
///
public string GetHtml(string urlStr)
{
try
{
WebRequest request = WebRequest.Create(urlStr);
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader sr = new StreamReader(stream, Encoding.Default);
string html = sr.ReadToEnd();
return html;
}
catch
{
return "没有匹配";
}
finally
{ }
}
#endregion
#region "获取匹配"
public string[] macthValue(string regexStr, string htmlStr)
{
MatchCollection mc;
Regex r = new Regex(regexStr);
mc = r.Matches(htmlStr);
int i = 0;
int c = mc.Count;
string[] arrStr = new string[c];
//for (int i = 1; i < mc.Count+1 ; i++)
//{
// arrStr[i] = mc[i].Value;
// //MessageBox.Show("mc");
//}
foreach (Match m in mc)
{
arrStr[i] = m.Groups[0].Value;
i++;
}
return arrStr;
}
#endregion
public string htmlStr()
{
StreamReader sr = new StreamReader(@"C:/url/1.txt", System.Text.Encoding.GetEncoding("utf-8"));
string temp = sr.ReadToEnd();
sr.Close();
return temp;
}
public int i = 0;
private void timer1_Tick(object sender, EventArgs e)
{
if (i < LisBoxUrl.Items.Count - 1)
{
string[] urlStr = new string[] { };
urlStr = macthValue(@"http://group.xiaonei.com/GetThread.do\?id=\d+&parentpage=0&curpage=0&label=&tribeId=\d+", htmlStr());
wB.Navigate(urlStr[i].Replace("amp;", ""));
this.wB.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wB_DocumentCompleted);
int m = i % 3;
Random r = new Random();
switch (m)
{
case 0:
HtmlAdd("editorForm", "editorFormBtn", "cmtbody", this.textBox1.Text + r.Next(10).ToString());
break;
case 1:
HtmlAdd("editorForm", "editorFormBtn", "cmtbody", this.textBox2.Text + r.Next(10).ToString());
break;
case 2:
HtmlAdd("editorForm", "editorFormBtn", "cmtbody",this.textBox3.Text + r.Next(10).ToString());
break;
}
i++;
}
}
private void wB_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Interval = int.Parse(this.textBox4.Text )*1000;
MessageBox.Show("应用间隔" + this.textBox4.Text + "秒");
}
}
}