WinForm上网站自动登录时Cookie方面遇到的问题
代码如下:
string _loginContent,_loginURL;
//_loginURL= "http://www.cmfu.com/loginuser.asp ";
//_loginContent= "user_name=@username&pass_word=@password&ekey=&user_type=1 ";
_loginURL= "http://www.nch.com.tw/login.php ";
_loginContent= "key=login&orig_self=%2Fmybook.php&orig_query_string=&login_id=@username&login_pw=@password ";
string UserName= "vstest ";
string PassWord= "123456 ";
byte[] LoginContent = Encoding.ASCII.GetBytes(_loginContent.Replace( "@username ",UserName).Replace( "@password ",PassWord));
HttpWebResponse LoginResponse;
HttpWebRequest LoginRequest = (HttpWebRequest)WebRequest.Create(_loginURL);
LoginRequest.Method= "POST ";
LoginRequest.ContentType= "application/x-www-form-urlencoded ";
LoginRequest.ContentLength=LoginContent.Length;
LoginRequest.CookieContainer=new CookieContainer();
Stream streamContent=LoginRequest.GetRequestStream();
streamContent.Write(LoginContent,0,LoginContent.Length);
streamContent.Close();
LoginRequest.GetResponse();
现在遇到的问题是,当登陆cmfu时一切正常,LoginRequest.CookieContainer中就存有Cookie,可以用于访问书架,而登陆nch时则LoginRequest.CookieContainer中为空,这是怎么回事?
用Sniffer Pro可以看到,POST后确实返回了Cookie,用IE登录的话也确实有Cookie存在,为什么程序会得不到
求教
谢谢
[解决办法]
LoginRequest.CookieContainer=new CookieContainer();
这个要保存到一个全局变量
第二次Request的时候赋值过来才行
[解决办法]
HttpWebRequest LoginRequest = (HttpWebRequest)WebRequest.Create(_loginURL);
------------------------------------------------
HttpWebRequest LoginRequest = (HttpWebRequest)WebRequest.Create(new System.Uri((_loginURL));
用Sniffer Pro看到的POST的数据是这个吗??
_loginContent= "key=login&orig_self=%2Fmybook.php&orig_query_string=&login_id=@username&login_pw=@password ";