菜鸟求教高人:我用下面这段代码虚拟登陆,可是得不到登陆后的页面源代码,请帮忙看看是怎么回事,感激不尽!

hihijerry 2008-11-23 11:35:02
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Web;

namespace login
{
class Program
{
static void Main(string[] args)
{
string username = "jyytju04";
string password = "123456";

string postdata = "username=" + username + "&loginfield=username&password=" + password + "&loginsubmit=%E7%99%BB%E5%BD%95";
ASCIIEncoding encoding = new ASCIIEncoding();
//string postdata = "formhash=a0cfd704&referer=&loginfield=username&username=jyytju04&password=123456&questionid=0&answer=&cookietime=2592000&loginmode=&styleid=&loginsubmit=%E6%8F%90%E4%BA%A4";
string postData = postdata;
//postData += ("&Accept=" + strsubmit);
byte[] data = encoding.GetBytes(postData);
// Prepare web request...
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://bbs.ifeng.com/loggingutf8.php?action=login");
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream = myRequest.GetRequestStream();
// Send the data.
newStream.Write(data, 0, data.Length);
newStream.Close();
// Get response
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
string content = reader.ReadToEnd();
//Response.Write(content) ;
Console.WriteLine(content);
Console.ReadLine();
reader.Close();
}
}
}
...全文
101 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
D13ay 2008-11-27
  • 打赏
  • 举报
回复
帮顶
whitechololate 2008-11-27
  • 打赏
  • 举报
回复
传那么多参数!~
帮顶~
DavidNoWay 2008-11-27
  • 打赏
  • 举报
回复
mark
pvgyetg 2008-11-27
  • 打赏
  • 举报
回复
友情UP
leebo123 2008-11-26
  • 打赏
  • 举报
回复
呵呵 这个问题我原来也遇到过 登录网页需要传递cookies才能保持登录的状态 所以你要把cookies保存起来 我给你一点示例

private bool PostWebRequest()
{
CookieContainer cc = new CookieContainer(); //建立保存cookies的容器
string pos tData = "user=" + strUser + "&pass=" + strPsd;
byte[] byteArray = Encoding.UTF8.GetBytes(postData);

HttpWebRequest webRequest2 = (HttpWebRequest)WebRequest.Create(new Uri("/chk.asp"));
webRequest2.CookieContainer = cc; //设置cookies
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);
string text2 = sr2.ReadToEnd();
......
}


当然 你如果在打开其他页面的时候也要把这个cookies加上 这样就可以保持登录状态了 就好比登录论坛
MicroDeviser 2008-11-26
  • 打赏
  • 举报
回复
看看正常登陆后的内容有些什么没有
mjjzg 2008-11-23
  • 打赏
  • 举报
回复
对这个问题不是很清楚
帮你顶一下,以表支持
wenbin 2008-11-23
  • 打赏
  • 举报
回复
出现的因素可能比较多,先找个监视工具,看下post的时候都有哪些数据,格式,
一些设置等。

111,130

社区成员

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

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

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