一个登录论坛并上传帖子和文件的代码
以前的,现在准备删除,就这里存档了的。一般获取网页和登录用得着的。
下载地址:https://files.cnblogs.com/rq204/%E7%99%BB%E5%BD%95%E8%AE%BA%E5%9D%9B%E5%B9%B6%E5%8F%91%E8%A1%A8%E5%B8%96%E5%AD%90%E7%9A%84%E4%BB%A3%E7%A0%81.rar
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Text;
using
System.Windows.Forms;
namespace
BBSEasy
{
public
partial
class
Form1 : Form
{
public
Form1()
{
InitializeComponent();
}
private
System.Net.CookieCollection cookie =
new
System.Net.CookieCollection();
private
void
btnLogin_Click(
object
sender, EventArgs e)
{
if
(
this
.tbPassword.Text ==
""
||
this
.tbUserName.Text ==
""
)
return
;
string
posturl =
"http://www.aippt.cn/member.php?mod=logging&action=login&loginsubmit=yes&infloat=yes&lssubmit=yes&inajax=1"
;
System.Net.HttpWebRequest request =
this
.CreateHttp(posturl);
string
postdata =
"username="
+ ed(
this
.tbUserName.Text) +
"&cookietime=2592000&password="
+ ed(
this
.tbPassword.Text) +
"&quickforward=yes&handlekey=ls"
;
byte
[] bt = System.Text.Encoding.UTF8.GetBytes(postdata);
request.ContentLength = bt.Length;
System.IO.Stream sm = request.GetRequestStream();
sm.Write(bt, 0, bt.Length);
sm.Close();
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
string
html =
this
.GetHtml(response);
if
(html.Contains(
"window.location.href='http://www.aippt.cn/';<"
))
{
this
.cookie = response.Cookies;
MessageBox.Show(
"登录成功"
);
}
else
{
MessageBox.Show(html,
"登录失败"
);
}
}
private
void
Form1_Load(
object
sender, EventArgs e)
{
}
private
void
button2_Click(
object
sender, EventArgs e)
{
OpenFileDialog ofd =
new
OpenFileDialog();
ofd.Multiselect =
true
;
if
(ofd.ShowDialog() == DialogResult.OK)
{
this
.tbFile.Tag = ofd.FileNames;
this
.tbFile.Clear();
foreach
(
string
f
in
ofd.FileNames)
{
this
.tbFile.Text += f +
";"
;
}
}
}
private
string
ed(
string
s)
{
return
System.Web.HttpUtility.UrlEncode(s, System.Text.Encoding.UTF8);
}
private
void
btnPost_Click(
object
sender, EventArgs e)
{
if
(
this
.tbTitle.Text ==
""
||
this
.tbContent.Text ==
""
||
this
.tbFid.Text ==
""
)
return
;
string
fidurl =
"http://www.aippt.cn/forum.php?mod=post&action=newthread&fid="
+
this
.tbFid.Text;
System.Net.HttpWebRequest hwr =
this
.CreateHttp(fidurl);
hwr.Method =
"GET"
;
hwr.CookieContainer =
new
System.Net.CookieContainer();
hwr.CookieContainer.Add(
this
.cookie);
System.Net.HttpWebResponse hwrs = (System.Net.HttpWebResponse)hwr.GetResponse();
string
html =
this
.GetHtml(hwrs);
string
formhash =
this
.getInput(html,
"\"formhash"
);
string
posttime =
this
.getInput(html,
"posttime"
);
string
hash =
this
.getInput(html,
"\"hash"
);
string
uid =
this
.getInput(html,
"\"uid"
);
string
posturl =
"http://www.aippt.cn/forum.php?mod=post&action=newthread&fid="
+
this
.tbFid.Text +
"&extra=&topicsubmit=yes"
;
System.Net.HttpWebRequest request =
this
.CreateHttp(posturl);
request.CookieContainer =
new
System.Net.CookieContainer();
request.CookieContainer.Add(
this
.cookie);
request.AllowAutoRedirect =
false
;
string
postdata =
"formhash="
+ formhash +
"&posttime="
+ posttime +
"&wysiwyg=1&subject="
+ ed(
this
.tbTitle.Text) +
"&message="
+ ed(
this
.tbContent.Text) +
"&replycredit_extcredits=0&replycredit_times=1&replycredit_membertimes=1&replycredit_random=100&tags=&save=&uploadalbum=-2&albumcatid=1&newalbum=%E8%AF%B7%E8%BE%93%E5%85%A5%E7%9B%B8%E5%86%8C%E5%90%8D%E7%A7%B0&usesig=1&allownoticeauthor=1"
;
if
(
this
.tbFile.Tag !=
null
)
{
string
[] files = (
string
[])
this
.tbFile.Tag;
foreach
(
string
ff
in
files)
{
try
{
string
aid =
this
.UploadFile(ff,
this
.tbFid.Text, uid, hash);
postdata +=
"&"
+ ed(
"attachnew["
+ aid +
"][description]"
) +
"="
+ ed(System.IO.Path.GetFileName(ff));
}
catch
(Exception ex)
{
MessageBox.Show(ex.Message);
return
;
}
}
}
byte
[] bt = System.Text.Encoding.UTF8.GetBytes(postdata);
request.ContentLength = bt.Length;
System.IO.Stream sm = request.GetRequestStream();
sm.Write(bt, 0, bt.Length);
sm.Close();
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
html =
this
.GetHtml(response);
if
(response.StatusCode == System.Net.HttpStatusCode.Moved)
{
MessageBox.Show(
"发布成功"
);
}
else
{
MessageBox.Show(html,
"发布失败"
);
}
}
private
string
getInput(
string
html,
string
name)
{
string
start = name +
"\" value=\""
;
int
pos = html.IndexOf(start);
if
(pos > 0)
{
int
pos2 = html.IndexOf(
"\""
, pos + start.Length);
if
(pos2 > 0)
{
return
html.Substring(pos + start.Length, pos2 - pos - start.Length);
}
}
return
""
;
}
private
System.Net.HttpWebRequest CreateHttp(
string
url)
{
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url);
request.Method =
"POST"
;
request.UserAgent =
"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20100101 Firefox/16.0"
;
request.Proxy = System.Net.HttpWebRequest.GetSystemWebProxy();
request.Referer =
"http://www.aippt.cn/"
;
request.CookieContainer =
new
System.Net.CookieContainer();
request.ServicePoint.Expect100Continue =
false
;
request.ContentType =
"application/x-www-form-urlencoded"
;
request.AutomaticDecompression = System.Net.DecompressionMethods.GZip;
request.UserAgent =
"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20100101 Firefox/16.0"
;
request.Proxy = System.Net.HttpWebRequest.GetSystemWebProxy();
request.Referer =
"http://www.aippt.cn/"
;
request.ServicePoint.Expect100Continue =
false
;
request.ContentType =
"application/x-www-form-urlencoded"
;
request.AutomaticDecompression = System.Net.DecompressionMethods.GZip;
return
request;
}
private
string
GetHtml(System.Net.HttpWebResponse response)
{
System.IO.Stream rsm = response.GetResponseStream();
System.IO.StreamReader sr =
new
System.IO.StreamReader(rsm, System.Text.Encoding.UTF8);
string
html = sr.ReadToEnd();
sr.Close();
rsm.Close();
return
html;
}
private
string
UploadFile(
string
path,
string
fid,
string
uid,
string
hash)
{
string
posturl =
"http://www.aippt.cn/misc.php?mod=swfupload&action=swfupload&operation=upload&fid="
+ fid;
System.Net.HttpWebRequest request =
this
.CreateHttp(posturl);
string
boundary =
"----------haveggoodday343450"
;
request.ContentType =
"multipart/form-data; boundary="
+ boundary;
request.UserAgent =
"Shockwave Flash"
;
request.CookieContainer =
new
System.Net.CookieContainer();
request.CookieContainer.Add(
this
.cookie);
Dictionary<
string
,
string
> dic =
new
Dictionary<
string
,
string
>();
dic.Add(
"Filename"
, System.IO.Path.GetFileName(path));
dic.Add(
"uid"
, uid);
dic.Add(
"hash"
, hash);
dic.Add(
"Upload"
,
"Submit Query"
);
dic.Add(
"filetype"
, System.IO.Path.GetExtension(path));
StringBuilder sb =
new
StringBuilder();
foreach
(KeyValuePair<
string
,
string
> kv
in
dic)
{
sb.Append(
"--"
).Append(boundary).Append(
"\r\n"
).Append(
"Content-Disposition: form-data; name=\""
+ kv.Key +
"\"\r\n\r\n"
).Append(ed(kv.Value)).Append(
"\r\n"
);
}
sb.Append(
"--"
).Append(boundary).Append(
"\r\nContent-Disposition: form-data; name=\"Filedata\"; filename=\""
).Append(System.IO.Path.GetFileName(path)).Append(
"\"\r\nContent-Type: application/octet-stream\r\n\r\n"
);
byte
[] bt1 = System.Text.Encoding.UTF8.GetBytes(sb.ToString());
System.IO.FileStream fs =
new
System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
byte
[] bt2 =
new
byte
[fs.Length];
fs.Read(bt2, 0, Convert.ToInt32(fs.Length));
byte
[] bt3 = System.Text.Encoding.UTF8.GetBytes(
"\r\n--"
+ boundary +
"--\r\n"
);
byte
[] bt =
new
byte
[bt1.Length + bt2.Length + bt3.Length];
bt1.CopyTo(bt, 0);
bt2.CopyTo(bt, bt1.Length);
bt3.CopyTo(bt, bt1.Length + bt2.Length);
fs.Close();
request.ContentLength = bt.Length;
System.IO.Stream sm = request.GetRequestStream();
sm.Write(bt, 0, bt.Length);
sm.Close();
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
string
html =
this
.GetHtml(response);
if
(System.Text.RegularExpressions.Regex.IsMatch(html,
"^\\d+$"
))
{
return
html;
}
else
throw
new
Exception(
"上传文件失败"
+ html);
}
}
}