c#用httpwebrequest实现自动发帖
c#用httpwebrequest实现自动发帖为什么会出现这种情况?中间有continue,源代码在下面,求教育大婶们!
POST /user/logout HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Host: www.tianya.cn
Content-Length: 352
Expect: 100-continue
Connection: Keep-Alive
HTTP/1.1 100 Continue
__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=/wEPDwULLTE1MzYzODg2NzZkGAEFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYBBQtjaGtSZW1lbWJlcm1QYDyKKI9af4b67Mzq2xFaL9Bt&__EVENTVALIDATION=/wEWBQLWwpqPDQLyj/OQAgK3jsrkBALR55GJDgKC3IeGDE1m7t2mGlasoP1Hd9hLaFoI2G05&tbUserName=hayabusa2014&tbPassword=cyj12130913gf&btnLogin=? ?&txtReturnUrl=http://home.cnblogs.com/HTTP/1.1 200 OK
Server: nginx
Date: Mon, 11 Aug 2014 06:08:28 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding
代码:
private void btnSubmit_Click(object sender, EventArgs e)
{
string postData = string.Format("__EVENTTARGET=Editor$Edit$lkbPost&__EVENTARGUMENT=&__VIEWSTATE=&Editor$Edit$txbTitle={0}&Editor$Edit$EditorBody={1}&Editor$Edit$APOptions$APSiteHome$chkDisplayHomePage=on&Editor$Edit$Advanced$ckbPublished=on&Editor$Edit$Advanced$chkComments=on&Editor$Edit$Advanced$chkMainSyndication=on&Editor$Edit$Advanced$txbEntryName=&Editor$Edit$Advanced$txbExcerpt=&Editor$Edit$Advanced$txbTag=&Editor$Edit$Advanced$tbEnryPassword=",txtTitle.Text,rtxtContent.Text);
string Url = @"http://bbs.tianya.cn/compose.jsp?item=funinfo&sub=全部";
ASCIIEncoding encoding=new ASCIIEncoding ();
byte[] byt = encoding.GetBytes(postData);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "Post";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byt.Length;
request.KeepAlive = true;
request.CookieContainer = Ccontainter; //返回的cookie会附加在这个容器里面
//发送数据
Stream newStream = request.GetRequestStream();
newStream.Write(byt, 0, byt.Length);
//以下俩句不可缺少
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
newStream = response.GetResponseStream();
StreamReader reader = new StreamReader(newStream, Encoding.UTF8);
string text = reader.ReadToEnd();
reader.Close();
newStream.Close();
if (text.IndexOf("发布成功") > 0)
{
MessageBox.Show("发布成功!");
txtTitle.Text = "";
rtxtContent.Text = "";
}
else
{
MessageBox.Show("发布成功!");
}
}