HttpWebRequest“POST”登陆返回错误

stonehalf 2011-12-27 02:41:23
我在使用如下代码来登陆网站,

使用WebRequest.Create一个 https://dynamic..../loginAction.do?method=init

System.IO.Stream outputStream = webRequest.GetRequestStream();
outputStream.Write(byteArray, 0, postData.Length);
之后

使用HttpAnalyzer查看返回情况,从返回的内容来看,怎么返回的是填好登陆信息的登陆页面呢?

也就是返回还是登陆页面,只是填好了登陆信息而已

还想请家伙帮忙看看?


private void LOGIN_Click(object sender, EventArgs e)
{
string getname = NameTextBox.Text.ToString();
string getpwd = PwdTextBox.Text.ToString();
string getrandcoe = randcodebox.Text.ToString();
PostWebRequest(getname, getpwd, getrandcoe);
}
private void PostWebRequest(string name, string pswd, string randcode)
{
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback((a, b, c, d) => { return true; });
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
string postData = "loginUser.user_name=" + name + "&ErrorFocus=&user.password=" + pswd + "&passwordErrorFocus=&randCode="+ randcode + "&randErrorFocus=";
byte[] byteArray = Encoding.UTF8.GetBytes(postData); // 转化

CookieContainer cookieContainer = new CookieContainer();

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri("https://dynamic..../loginAction.do?method=init"));
webRequest.Headers.Add("accept-language", "zh-cn,zh;q=0.5");
webRequest.Headers.Add("accept-encoding", "gzip,deflate");
webRequest.Headers.Add("accept-charset", "GB2312,utf-8;q=0.7,*;q=0.7");
webRequest.Headers.Add("cache-control", "max-age=0");
webRequest.Headers.Add("keep-alive", "115");
webRequest.CookieContainer = cookieContainer;
webRequest.Referer = "https://dynamic...";
webRequest.Accept = "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; TencentTraveler 4.0; .NET CLR 2.0.50727; .NET CLR 1.1.4322; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; InfoPath.1)";
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.ContentLength = postData.Length;

// 提交请求数据
System.IO.Stream outputStream = webRequest.GetRequestStream();
outputStream.Write(byteArray, 0, postData.Length);
outputStream.Close();
try
{
// 接收返回的页面
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
System.IO.Stream responseStream = response.GetResponseStream();
System.IO.StreamReader reader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
if (response.Cookies.Count > 0)
cookieContainer.Add(response.Cookies);
string srcString = reader.ReadToEnd();
responseStream.Flush();
responseStream.Close();
response.Close();
}
catch (WebException ex)
{
MessageBox.Show(ex.Message);
}

}



...全文
72 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
hui717 2011-12-28
  • 打赏
  • 举报
回复
帮顶,这个要详细研究下http协议了
stonehalf 2011-12-28
  • 打赏
  • 举报
回复
没人看??? 大伙帮帮忙咯
stonehalf 2011-12-28
  • 打赏
  • 举报
回复
我回头再试试看,估计找到原因了
stonehalf 2011-12-27
  • 打赏
  • 举报
回复
我看到回复次数为2

最后是hefeng_aspnet回复的,点进来却看不到回复呢?
stonehalf 2011-12-27
  • 打赏
  • 举报
回复
我看到回复次数为2

最后是hefeng_aspnet回复的,点进来却看不到回复呢?

111,131

社区成员

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

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

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