用WebRequest登录页面的时候没有登录成功

bendan999999999 2006-08-07 04:24:37
今天在用WebRequest登录页面的时候总是登录不成功,那个网站是使用aspx做的,我所有控件的值都传了,但是还是登录不了,我在本地测试是可以的,有没有哪位达人可以告诉我,可能是什么方面的问题啊?
...全文
389 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
tangd 2006-08-07
  • 打赏
  • 举报
回复
都挺正确的呀.UP一下.
bendan999999999 2006-08-07
  • 打赏
  • 举报
回复
以上是主要代码。
bendan999999999 2006-08-07
  • 打赏
  • 举报
回复
string LOGIN_URL = @"http://www.pdhr.com/login.aspx";
string SECRET_PAGE_URL = "http://www.pdhr.com/enterprise/EnterpriseToday.aspx?MenuID=7";

HttpWebRequest webRequest = WebRequest.Create(LOGIN_URL) as HttpWebRequest;
StreamReader responseReader = new StreamReader(
webRequest.GetResponse().GetResponseStream()
);
string responseData = responseReader.ReadToEnd();
responseReader.Close();

// extract the viewstate value and build out POST data
string viewState = ExtractViewState(responseData);
string postData =
String.Format(
"__VIEWSTATE={0}&UserLogin1:S_tbloginnamee={1}&UserLogin1:S_tbpassword={2}&UserLogin1:loginradio={3}&UserLogin1:S_btconfirm={4}",
viewState, "敏泰", "123456", "S_rbperson", "确 认"
);

// have a cookie container ready to receive the forms auth cookie
CookieContainer cookies = new CookieContainer();
CookieContainer reqcookies = new CookieContainer();

// now post to the login form
webRequest = WebRequest.Create(LOGIN_URL) as HttpWebRequest;
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)";
webRequest.CookieContainer = cookies;

// write the form values into the request message
StreamWriter requestWriter = new StreamWriter(webRequest.GetRequestStream());
requestWriter.Write(postData);
requestWriter.Close();

// we don't need the contents of the response, just the cookie it issues
webRequest.GetResponse().Close();
CookieCollection cookieheader = webRequest.CookieContainer.GetCookies(new Uri(LOGIN_URL));


// now we can send out cookie along with a request for the protected page
webRequest = WebRequest.Create(SECRET_PAGE_URL) as HttpWebRequest;

webRequest.CookieContainer = cookies;
webRequest.Referer = SECRET_PAGE_URL;
webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)";
webRequest.ContentType = "application/x-www-form-urlencoded";
responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream(),Encoding.GetEncoding("gb2312"));
cookieheader = webRequest.CookieContainer.GetCookies(new Uri(SECRET_PAGE_URL));

// and read the response
responseData = responseReader.ReadToEnd();
responseReader.Close();

Response.Write(responseData);
terry_12 2006-08-07
  • 打赏
  • 举报
回复
是不是有头验证之类的?

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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