关于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;
}
...全文
464 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
thoughter 2010-07-22
  • 打赏
  • 举报
回复
CookieContainer cc = new CookieContainer();
每次SubMit_ww都是一个新的CookieContainer,你的登录就白做了
ffww 2010-07-22
  • 打赏
  • 举报
回复
提交后返回代码:

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<meta HTTP-EQUIV="Content-Type" content="text/html; charset=gb2312">
</HEAD>
<BODY bgcolor="#F5F9FA" rightMargin=50 leftMargin=50 topMargin=3 id="tech_body" class="body2"><script language="Javascript">alert('用户口令有误!');window.location='javascript:history.go(-1)';</script>
事理 2010-07-22
  • 打赏
  • 举报
回复
看那些参数每次提交都有不同的值,那么这些参数就要传递过去,不然就登陆不成功,如果遇到js中产生的参数,那么就要用webrequest获取set-cookie的头部信息来做,我这个也没太搞懂。ieHTTPHeaders这个抓包很好。

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧