关于POST数据登陆网站的问题

ioirliao16 2011-06-30 05:00:59
http://bbs.egou.com/logging.php?action=login&loginsubmit=yes&floatlogin=yes&inajax=1&formhash=c7169c45&loginfield=username&username=tempUser&password=123456789&questionid=0&answer=
红色那段是需要POST的数据,上面的url我直接在浏览器打开,返回以下html

</script><script type="text/javascript" reload="1">
display('loginfield_selectinput');
display('loginform');
pagescroll.right(2);
$('messageleft').innerHTML = '<h1>欢迎您回来 新兵 tempUser</h1>';
$('messageright').innerHTML = '<h1><a href="javascript:;" onclick="location.reload()">如果该页面长时间没有响应,请点这里刷新</a></h1>';
setTimeout('location.reload()', 3000);
floatwinreset = 1;
</script>
</div>

</div></div></div>

</div>

我自己写了段代码返回的却不是上面的信息代码如下请指教,谢谢:

void LoginBtnClick(object sender, EventArgs e)
{
Encoding coder=Encoding.GetEncoding("utf-8");
string str="formhash=8c44a714&loginfield=username&username="+
userText.Text+"&password="+passText.Text+
"&questionid=0&answer=&loginsubmit=true";
byte[] postByte=coder.GetBytes(str);
HttpWebRequest hwq=(HttpWebRequest)WebRequest.Create("http://bbs.egou.com");
hwq.Method="POST";
hwq.ContentType="keep-alive";
hwq.Accept="application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
hwq.UserAgent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.237 Safari/534.10";
hwq.Referer="http://bbs.egou.com/logging.php?action=login";
hwq.RequestUri.
hwq.ContentLength=postByte.Length;
Stream postStream=hwq.GetRequestStream();
postStream.Write(postByte,0,postByte.Length);

WebResponse wrp=hwq.GetResponse();
Stream receiveStream=wrp.GetResponseStream();
StreamReader reader=new StreamReader(receiveStream,Encoding.GetEncoding("gb2312"));
string s=string.Empty;
while(!reader.EndOfStream){
s+=reader.ReadLine();
Application.DoEvents();
}
richTextBox1.AppendText(s);
}
...全文
270 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
ioirliao16 2011-06-30
  • 打赏
  • 举报
回复
Quote=引用 22 楼 fangxinggood 的回复:]
浏览器什么动作,你的代码就是什么动作。

点击登录时,弹出输入用户名和密码页面,--- 这是GET
然后你输入用户名和密码提交, --- 这是POST

始终一个页面,地址当然相同。
[/Quote]
原来是这样,谢谢!不过现在又遇到一个问题了,就是发帖子的问题
开了个新帖,地址:http://topic.csdn.net/u/20110630/23/d2c1ee06-0947-4e90-afb9-b71818cdaf04.html
返回的信息是:

<?xml version="1.0" encoding="gbk"?><root><![CDATA[<script type="text/javascript" reload="1">
var nopermst
</script>
<div>
<h3 class="float_ctrl"><em></em><span><a href="javascript:;" class="float_close" onclick="clearTimeout(nopermst);floatwinreset = 1;floatwin('close_fastpost');" title="关闭">关闭</a></span></h3>
<div class="float_message">您的请求来路不正确,无法提交。<script type="text/javascript" reload="1">
if(typeof messagehandle_fastpost =='function') {messagehandle_fastpost('0', '您的请求来路不正确,无法提交。');}
</script><br /><br />
<button onclick="floatwinreset = 1;floatwin('close_fastpost');">关闭</button>

</div>
</div>
]]></root>

代码如下:

void PostMessage(string text){
HttpWebRequest webRequest=WebRequest.Create("http://bbs.egou.com/post.php?action=reply&fid=19&tid=1222290&extra=page%3D1&replysubmit=yes&infloat=yes&handlekey=fastpost&inajax=1") as HttpWebRequest;
webRequest.Method="POST";
webRequest.Accept="image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/x-ms-xbap, application/x-ms-application, */*";
webRequest.Referer="http://bbs.egou.com/viewthread.php?tid=1222290&extra=page%3D1";
webRequest.UserAgent="Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; BTRS86335; .NET4.0C; TheWorld)Content-Type:application/x-www-form-urlencoded";
// webRequest.Connection="Keep-Alive";
webRequest.CookieContainer=webInof.cookieContainer;
string pst="formhash="+webInof.formhash+"&subject=&usesig=1&message=mark%21%21%21%21%21%21%21%21%21%21%21%21%21%21%21%21%21";
byte[] b=Encoding.ASCII.GetBytes(pst);
Stream requestStrem=webRequest.GetRequestStream();
requestStrem.Write(b,0,b.Length);
requestStrem.Close();
HttpWebResponse webResponse=webRequest.GetResponse() as HttpWebResponse;
Stream responseStream=webResponse.GetResponseStream();
StreamReader responseReader=new StreamReader(responseStream,Encoding.GetEncoding("gb2312"));
richTextBox1.Clear();
richTextBox1.AppendText(responseReader.ReadToEnd());
}
机器人 2011-06-30
  • 打赏
  • 举报
回复
浏览器什么动作,你的代码就是什么动作。

点击登录时,弹出输入用户名和密码页面,--- 这是GET
然后你输入用户名和密码提交, --- 这是POST

始终一个页面,地址当然相同。
ioirliao16 2011-06-30
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 fangxinggood 的回复:]
HttpWebRequest hwq=(HttpWebRequest)WebRequest.Create("http://bbs.egou.com");

改为:

HttpWebRequest hwq=(HttpWebRequest)WebRequest.Create("http://bbs.egou.com/logging.php?action=login&infloat=ye……
[/Quote]
大神啊,能否给我再上一课,为什么要这样的?
ioirliao16 2011-06-30
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 fangxinggood 的回复:]
HttpWebRequest hwq=(HttpWebRequest)WebRequest.Create("http://bbs.egou.com");

改为:

HttpWebRequest hwq=(HttpWebRequest)WebRequest.Create("http://bbs.egou.com/logging.php?action=login&infloat=ye……
[/Quote]
大神啊,真不愧是大神!分全数给您了。
机器人 2011-06-30
  • 打赏
  • 举报
回复
HttpWebRequest hwq=(HttpWebRequest)WebRequest.Create("http://bbs.egou.com");

改为:

HttpWebRequest hwq=(HttpWebRequest)WebRequest.Create("http://bbs.egou.com/logging.php?action=login&infloat=yes&handlekey=login&inajax=1&ajaxtarget=floatwin_login");

GET 和 POST 应该是相同的地址
ioirliao16 2011-06-30
  • 打赏
  • 举报
回复
大神们帮帮我吧。。。
ioirliao16 2011-06-30
  • 打赏
  • 举报
回复
哪位大神救救我啊,开新帖子再加一百分。
ioirliao16 2011-06-30
  • 打赏
  • 举报
回复
我如此写了,依然不行,哪位大神可以给出个可行的demo我吗?谢谢!

struct WebInfo{
public string formhash;
public CookieContainer cookieContainer;
}
WebInfo GetWebInfo(string url){
WebInfo webInfo=new MainForm.WebInfo();
HttpWebRequest webRequest=(HttpWebRequest)WebRequest.Create(url);
CookieContainer cookieC=new CookieContainer();
cookieC.GetCookies(webRequest.RequestUri);
webInfo.cookieContainer=cookieC;
HttpWebResponse webResponse=(HttpWebResponse)webRequest.GetResponse();
Stream responseStream=webResponse.GetResponseStream();
StreamReader responseReader=new StreamReader(responseStream,Encoding.GetEncoding("gb2312"));
string str=responseReader.ReadToEnd();
string formhash=str.Substring(str.IndexOf("formhash=")+9,8);
webInfo.formhash=formhash;
return webInfo;
}
void LoginBtnClick(object sender, EventArgs e)
{
WebInfo webInof;
webInof=GetWebInfo("http://bbs.egou.com/logging.php?action=login&infloat=yes&handlekey=login&inajax=1&ajaxtarget=floatwin_login");
Encoding coder=Encoding.GetEncoding("utf-8");
string str="formhash="+webInof.formhash+"&loginfield=username&username="+
userText.Text+"&password="+passText.Text+
"&questionid=0&answer=&loginsubmit=true";
byte[] postByte=coder.GetBytes(str);
HttpWebRequest hwq=(HttpWebRequest)WebRequest.Create("http://bbs.egou.com");
hwq.CookieContainer=webInof.cookieContainer;
hwq.Method="POST";
hwq.ContentType="keep-alive";
hwq.Accept="application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
hwq.UserAgent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.237 Safari/534.10";
hwq.Referer="http://bbs.egou.com/logging.php?action=login";
hwq.ContentLength=postByte.Length;
Stream postStream=hwq.GetRequestStream();
postStream.Write(postByte,0,postByte.Length);
postStream.Close();

HttpWebResponse wrp=(HttpWebResponse)hwq.GetResponse();

Stream receiveStream=wrp.GetResponseStream();
StreamReader reader=new StreamReader(receiveStream,Encoding.GetEncoding("gb2312"));
string s=string.Empty;
while(!reader.EndOfStream){
s+=reader.ReadLine();
Application.DoEvents();
}
richTextBox1.Clear();
richTextBox1.AppendText(s);
}
solohac 2011-06-30
  • 打赏
  • 举报
回复
楼上的高手都说中了,2个问题
1.没设置cookieContainer,所以你的POST包可能没有发cookie
2.formhash 是每次登陆都不同的,到前面的包里去找
机器人 2011-06-30
  • 打赏
  • 举报
回复
formhash 记着替换成GET获得到的值。
机器人 2011-06-30
  • 打赏
  • 举报
回复
先GET访问 http://bbs.egou.com/logging.php?action=login&infloat=yes&handlekey=login&inajax=1&ajaxtarget=floatwin_login
要获取Cookie 和 formhash (这个formhash不是固定的)
formhash 在Response里:<input type="hidden" name="formhash" value="7a013652" />
然后再提交这个页面POST你上面红色字体的数据。
  • 打赏
  • 举报
回复
懒的看那么多代码了。你提交的cookie中的值怎么知道不是过期作废的呢?
ioirliao16 2011-06-30
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 xingchen_22 的回复:]
那你想干什么,我看你的意思就是想知道为什么不一样
[/Quote]
就是想用post的方式登陆一个论坛,然后发帖子。
小笨熊 2011-06-30
  • 打赏
  • 举报
回复
那就debug,看看你的url和你访问的那个url是否一样吧。
ioirliao16 2011-06-30
  • 打赏
  • 举报
回复
如下图片
ioirliao16 2011-06-30
  • 打赏
  • 举报
回复
如下图片
Forget 2011-06-30
  • 打赏
  • 举报
回复
那你想干什么,我看你的意思就是想知道为什么不一样
ioirliao16 2011-06-30
  • 打赏
  • 举报
回复
我是新手,我也不太明白,我是用Wireshark抓包工具分析出来,登陆的时候是post上去的。
小笨熊 2011-06-30
  • 打赏
  • 举报
回复
虽然我不是搞C#,但是我觉得很疑惑,lz通过输入url获得数据的这种方式是get啊!post方法需要代码或者插件才可以获取post事件。你是不是吧事件的出来方式搞错了呢?
ioirliao16 2011-06-30
  • 打赏
  • 举报
回复
那我应该怎么改呢?
加载更多回复(3)

110,533

社区成员

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

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

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