想实现一个模拟登录,但是无论如何还是登录不到系统中,请各位高手帮哈忙,我的代码如下:我用firebug查看了请求的数据如下:
post部分:
参数 application/x-www-form-urlencoded
----------------------------
__VIEWSTATE /wEPDwULLTExNDY5NzI1NTRkZDxUq+DKozsyBkD5NwEXp3n8vlje
btnLogin
txtPwd 123
txtUserName admin下面有一个提示源代码:__VIEWSTATE=%2FwEPDwULLTExNDY5NzI1NTRkZDxUq%2BDKozsyBkD5NwEXp3n8vlje&txtUserName=admin&btnLogin=&txtPwd=123
string loginurl = "http://www.******.com/login.aspx";
string postData = "__VIEWSTATE=%2FwEPDwULLTExNDY5NzI1NTRkZM0rikwJ7c5wXU5YQWwcCccBzYZE&txtUserName=admin&btnLogin=&txtPwd=123"; //这个是我根据post数据来弄的
byte[] b = System.Text.Encoding.GetEncoding("gb2312").GetBytes(postData);
wreq = HttpWebRequest.Create(loginurl) as HttpWebRequest ;
wreq.ContentLength = b.Length;
wreq.Method = "Post";
wreq.CookieContainer = ck;
wreq.ContentType = "text/html; charset=gb2312";
wreq.KeepAlive = true;
wreq.AllowAutoRedirect = true;
wreq.Referer = "http://www.******.com/login.aspx";
wreq.AllowWriteStreamBuffering = true;
wreq.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:8.0.1) Gecko/20100101 Firefox/8.0.1"; Stream myRequestStream = wreq.GetRequestStream();
using (StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312")))
{
myStreamWriter.Write(postData);
}
myRequestStream.Close(); using( wres=(HttpWebResponse) wreq.GetResponse()) {
//wres.Cookies永远都是为0
if (wres.Cookies.Count >0)
{
ck.Add(wres.Cookies);
}
using (StreamReader reader = new StreamReader(wres.GetResponseStream(),System.Text.Encoding.UTF8)) {
string aaa= reader.ReadToEnd();
Response.Write(aaa); //这里得到的数据还是登录页面的,而不是进入系统的代码, } }其它头信息如下:响应头信息原始头信息
Cache-Control private
Content-Length 146
Content-Type text/html; charset=utf-8
Date Fri, 02 Mar 2012 12:30:54 GMT
Location /manage/AdminDefault.aspx
Server Microsoft-IIS/6.0
X-AspNet-Version 2.0.50727
X-Powered-By ASP.NET
请求头信息原始头信息
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Charset GB2312,utf-8;q=0.7,*;q=0.7
Accept-Encoding gzip, deflate
Accept-Language zh-cn,zh;q=0.5
Connection keep-alive
Cookie ASP.NET_SessionId=3y15jsbfuf4irj451megtayu; cnzz_a3162184=2; sin3162184=; rtime=0; ltime=1330688762546; cnzz_eid=32735649-1330674004-
Host www.******.com
Referer http://www.******.com/login.aspx
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:8.0.1) Gecko/20100101 Firefox/8.0.1
来自缓存的响应头信息
Cache-Control private
Content-Length 146
Content-Type text/html; charset=utf-8
Date Fri, 02 Mar 2012 12:30:54 GMT
Location /manage/AdminDefault.aspx
Server Microsoft-IIS/6.0
X-AspNet-Version 2.0.50727
X-Powered-By ASP.NET
Request Headers From Upload Stream
Content-Length 107
Content-Type application/x-www-form-urlencoded请高手帮帮忙
post部分:
参数 application/x-www-form-urlencoded
----------------------------
__VIEWSTATE /wEPDwULLTExNDY5NzI1NTRkZDxUq+DKozsyBkD5NwEXp3n8vlje
btnLogin
txtPwd 123
txtUserName admin下面有一个提示源代码:__VIEWSTATE=%2FwEPDwULLTExNDY5NzI1NTRkZDxUq%2BDKozsyBkD5NwEXp3n8vlje&txtUserName=admin&btnLogin=&txtPwd=123
string loginurl = "http://www.******.com/login.aspx";
string postData = "__VIEWSTATE=%2FwEPDwULLTExNDY5NzI1NTRkZM0rikwJ7c5wXU5YQWwcCccBzYZE&txtUserName=admin&btnLogin=&txtPwd=123"; //这个是我根据post数据来弄的
byte[] b = System.Text.Encoding.GetEncoding("gb2312").GetBytes(postData);
wreq = HttpWebRequest.Create(loginurl) as HttpWebRequest ;
wreq.ContentLength = b.Length;
wreq.Method = "Post";
wreq.CookieContainer = ck;
wreq.ContentType = "text/html; charset=gb2312";
wreq.KeepAlive = true;
wreq.AllowAutoRedirect = true;
wreq.Referer = "http://www.******.com/login.aspx";
wreq.AllowWriteStreamBuffering = true;
wreq.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:8.0.1) Gecko/20100101 Firefox/8.0.1"; Stream myRequestStream = wreq.GetRequestStream();
using (StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312")))
{
myStreamWriter.Write(postData);
}
myRequestStream.Close(); using( wres=(HttpWebResponse) wreq.GetResponse()) {
//wres.Cookies永远都是为0
if (wres.Cookies.Count >0)
{
ck.Add(wres.Cookies);
}
using (StreamReader reader = new StreamReader(wres.GetResponseStream(),System.Text.Encoding.UTF8)) {
string aaa= reader.ReadToEnd();
Response.Write(aaa); //这里得到的数据还是登录页面的,而不是进入系统的代码, } }其它头信息如下:响应头信息原始头信息
Cache-Control private
Content-Length 146
Content-Type text/html; charset=utf-8
Date Fri, 02 Mar 2012 12:30:54 GMT
Location /manage/AdminDefault.aspx
Server Microsoft-IIS/6.0
X-AspNet-Version 2.0.50727
X-Powered-By ASP.NET
请求头信息原始头信息
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Charset GB2312,utf-8;q=0.7,*;q=0.7
Accept-Encoding gzip, deflate
Accept-Language zh-cn,zh;q=0.5
Connection keep-alive
Cookie ASP.NET_SessionId=3y15jsbfuf4irj451megtayu; cnzz_a3162184=2; sin3162184=; rtime=0; ltime=1330688762546; cnzz_eid=32735649-1330674004-
Host www.******.com
Referer http://www.******.com/login.aspx
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:8.0.1) Gecko/20100101 Firefox/8.0.1
来自缓存的响应头信息
Cache-Control private
Content-Length 146
Content-Type text/html; charset=utf-8
Date Fri, 02 Mar 2012 12:30:54 GMT
Location /manage/AdminDefault.aspx
Server Microsoft-IIS/6.0
X-AspNet-Version 2.0.50727
X-Powered-By ASP.NET
Request Headers From Upload Stream
Content-Length 107
Content-Type application/x-www-form-urlencoded请高手帮帮忙
而且 wreq 也没看见传递用户信息呢
验证通过后,服务器会向客户端发送cookie数据.用于在cookie中保留登录信息.即使是SESSION,大多也需要在cookie中保存sessionID.
但你没有做到这一步,没有获取服务器返回的这些cookie包,并把cookie数据连同验证成功后,需要跳转的网址一起再次发给服务器.
貌似有客户端是没有办法伪造一个cache,弄了好久,只看到网上有人模拟登录成功,但我却怎么弄都开不好,纠结