[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DotNet.Utilities;
using System.Text.RegularExpressions;//添加HttpHelper类的引用
namespace myDiscuz
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string formhash = "";
string cookie = "";
string saltkey = "";
string auth = "";
string encoding = "";
string domain = "";
private void Form1_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 获得字符串中开始和结束字符串中间得值
/// </summary>
/// <param name="str">字符串</param>
/// <param name="s">开始</param>
/// <param name="e">结束</param>
/// <returns></returns>
public static string GetValue(string str, string s, string e)
{
Regex rg = new Regex("(?<=(" + s + "))[.\\s\\S]*?(?=(" + e + "))", RegexOptions.Multiline | RegexOptions.Singleline);
return rg.Match(str).Value;
}
private void btnLogin_Click(object sender, EventArgs e)
{
domain = textBox3.Text.Trim();
string url = string.Format("{0}/member.php?mod=logging&action=login&loginsubmit=yes&infloat=yes&lssubmit=yes&inajax=1",domain);
string postData = string.Format("fastloginfield=username&username={0}&cookietime=2592000&password={1}&quickforward=yes&handlekey=ls", txtUser.Text.Trim(), txtPWD.Text.Trim());
HttpHelper http=new HttpHelper();
HttpItem item = new HttpItem()
{
URL=url,
Method="post",
ContentType = "application/x-www-form-urlencoded",
Referer = string.Format("{0}/forum.php", domain),
Postdata=postData,
};
HttpResult result = http.GetHtml(item);
Regex reg = new Regex(@"\w{4}_\d{4}_saltkey=\S*;");//out&formhash=(.*)\">" (.*)中的值是需要的
MatchCollection mts = reg.Matches(result.Cookie);
foreach (Match itemformhash in mts)
{
saltkey = itemformhash.Value;
}
reg = new Regex(@"\w{4}_\d{4}_auth=\S*;");
mts = reg.Matches(result.Cookie);
foreach (Match itemformhash in mts)
{
auth = itemformhash.Value;
}
cookie = saltkey + auth;
richTextBox1.Text = cookie;
if (result.Html.Contains("root"))
{
label8.Text = "登陆成功!";
btnLogin.Enabled = false;
}
else
{
label8.Text = "登录失败!";
}
item = new HttpItem()
{
URL = string.Format("{0}/forum.php", domain),
Cookie = cookie,
};
result = http.GetHtml(item);
encoding = GetValue(result.Html, "charset=", "\"");
formhash = GetValue(result.Html, "formhash\" value=\"", "\" />").Trim();
txtFormhash.Text = formhash;
}
private void button1_Click(object sender, EventArgs e)
{
long time = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
string posturl = string.Format("{0}/forum.php?mod=post&action=newthread&fid={1}&extra=&topicsubmit=yes",domain, textBox2.Text.Trim());
string postdata = string.Format("formhash={0}&posttime={1}&wysiwyg=1&typeid={2}&subject={3}&message={4}&replycredit_extcredits=0&replycredit_times=1&replycredit_membertimes=1&replycredit_random=100&readperm=&price=&tags=&rushreplyfrom=&rushreplyto=&rewardfloor=&replylimit=&stopfloor=&creditlimit=&allownoticeauthor=1&usesig=1&save=", formhash, time.ToString(), textBox4.Text.Trim(), textBox1.Text, richTextBox1.Text);
//string postdata = string.Format("formhash={0}&posttime={1}&wysiwyg=1&typeid={2}&subject={3}&message={4}&replycredit_extcredits=0&replycredit_times=1&replycredit_membertimes=1&replycredit_random=100&rushreplyfrom=&rushreplyto=&rewardfloor=&replylimit=&stopfloor=&creditlimit=&allownoticeauthor=1&addfeed=1&save=&connect_publish_t=0", formhash, time.ToString(), textBox4.Text.Trim(), textBox1.Text, richTextBox1.Text);
HttpHelper http = new HttpHelper();
HttpItem item = new HttpItem()
{
URL = posturl,
PostEncoding = System.Text.Encoding.GetEncoding(encoding.Trim()),
Method="post",
Postdata = postdata,
ContentType = "application/x-www-form-urlencoded",
Cookie = cookie,
};
HttpResult result = http.GetHtml(item);
richTextBox1.Text = result.Html;
item = new HttpItem()
{
URL =string.Format("{0}/forum.php?mod=forumdisplay&fid={1}",domain,textBox2.Text.Trim()),
Cookie = cookie,
};
result = http.GetHtml(item);
if (result.Html.Contains(textBox1.Text.Trim()))
{
label7.Text = "发帖成功!";
}
else
{
label7.Text = "发帖失败!";
}
}
}
}