关于HttpWebRequest的用法,高手们来看看@
ffww 2010-07-22 06:13:48 我想实现天涯社区的自动回贴,自己试了一下。登陆没问题,但回复贴子就不行了。不知道为什么不能成功
public void SubMit_ww(string Postdat, string Encod, string url, out string Txt)
{
try
{
byte[] byteArray;
CookieContainer cc = new CookieContainer();
if (Encod == "gb2312")
{
Encoding myEncoding = Encoding.GetEncoding("gb2312"); //编码方式:gb2312
byteArray = myEncoding.GetBytes(Postdat); // 转化
}
else
byteArray = Encoding.UTF8.GetBytes(Postdat); // 转化(编码方式:utf8)
HttpWebRequest webRequest2 = (HttpWebRequest)WebRequest.Create(new Uri(url));
webRequest2.CookieContainer = cc;
webRequest2.Method = "POST";
webRequest2.ContentType = "application/x-www-form-urlencoded";
webRequest2.ContentLength = byteArray.Length;
Stream newStream = webRequest2.GetRequestStream();
// Send the data.
newStream.Write(byteArray, 0, byteArray.Length); //写入参数
newStream.Close();
HttpWebResponse response2 = (HttpWebResponse)webRequest2.GetResponse();
StreamReader sr2 = new StreamReader(response2.GetResponseStream(), Encoding.Default);
Txt = sr2.ReadToEnd();
}
catch (Exception ex)
{
throw ex;
;
}
}
//-----------------------------------------------------------------------------------------------
//登陆 正常
private void button2_Click(object sender, EventArgs e)
{
//获取登录后的页面数据
string Txt = "";
SubMit_ww("vwriter=Colin疯子&vpassword=ffwwffww&returnURL=&fowardURL=", "gb2312", "http://login.tianya.cn/user/v3_loginsubmit.asp", out Txt);
richTextBox1.Text = Txt;
}
//-----------------------------------------------------------------------------------------------
//提交错得一塌糊涂,我用抓包工具抓到的数据全部原样送出,都不行,不知道为啥
private void button3_Click(object sender, EventArgs e)
{
string text="";
string tmp = "idItem=687&strItem=%C0%CB%C2%FE%C8%FD%D1%C7&chrAuthor=%D7%CF%C3%CE%BA%FB%B5%FB&idArticle=6876&strTitle=%3Cspan+class%3Dtitle_green%3E%C8%FD%D1%C7%D1%F4%B9%E2%B4%F3%BE%C6%B5%EA%B4%F3%D0%CD%C3%E2%B7%D1%BF%CD%B7%BF%C3%EB%C9%B1%BB%EE%B6%AF%A3%AC%B1%A8%C3%FB%D6%D0%A3%A1%3C%2Fspan%3E&idSign=1&pID=4&apn=103159%2C106228%2C109038%2C113789&pageAll=4&Submit=Response&ckey=429359575&cidWriter=40064597&strContent=%CE%D2%D2%B2%B1%A8%C3%FB%A1%A4%A3%A1&strAlbumPicURL=%C7%EB%CA%E4%C8%EB%C4%FA%D2%AA%B2%E5%C8%EB%CD%BC%C6%AC%B5%C4%C1%B4%BD%D3&PicDesc=%C7%EB%CA%E4%C8%EB%C4%FA%B6%D4%C9%CF%C3%E6%CD%BC%C6%AC%B5%C4%C3%E8%CA%F6%C4%DA%C8%DD";
SubMit_ww(tmp, "gb2312", "http://post.tianya.cn/new/techforum/content_submit.asp?idwriter=40064597&key=429359575&ttitem=687", out text);
richTextBox1.Text = text;
}