在使用HttpWebRequest模拟登陆网站的时候,如果网站的链接有做转向(Redirect)时,会丢失cookie的问题存在,请问下如何处理?
例子
如果网站转向时如下方式
Default.aspx=>A.aspx=>B.aspx=>C.aspx
在Default.aspx时设置
HttpCookie c = new HttpCookie("userName", "zb1");
c.Path = "/A";
this.Response.Cookies.Add(c);
this.Response.Redirect("A.aspx");
在A.aspx时设置
HttpCookie c = new HttpCookie("userName", "zb2");
c.Path = /A/B";
this.Response.Cookies.Add(c);
this.Response.Redirect("B.aspx");
在B.aspx时设置
HttpCookie c = new HttpCookie("userName", "zb3");
c.Path = "/A/B/C";
this.Response.Cookies.Add(c);
this.Response.Redirect("C.aspx"); 如果采用以下方式做模拟登陆时
private CookieContainer cc = new CookieContainer();
private void button1_Click(object sender, EventArgs e)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(@"http://localhost:1561/Default.aspx");
request.CookieContainer = cc;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string str2 = request.CookieContainer.GetCookieHeader(new Uri(@"http://localhost:1561/A/B/C"));
}
后发现我设置的cookie会丢失掉,如果设置cookie的domain也会发生这种情况。
我现在想取到不同路径和domain下的该如何处理哦
例子
如果网站转向时如下方式
Default.aspx=>A.aspx=>B.aspx=>C.aspx
在Default.aspx时设置
HttpCookie c = new HttpCookie("userName", "zb1");
c.Path = "/A";
this.Response.Cookies.Add(c);
this.Response.Redirect("A.aspx");
在A.aspx时设置
HttpCookie c = new HttpCookie("userName", "zb2");
c.Path = /A/B";
this.Response.Cookies.Add(c);
this.Response.Redirect("B.aspx");
在B.aspx时设置
HttpCookie c = new HttpCookie("userName", "zb3");
c.Path = "/A/B/C";
this.Response.Cookies.Add(c);
this.Response.Redirect("C.aspx"); 如果采用以下方式做模拟登陆时
private CookieContainer cc = new CookieContainer();
private void button1_Click(object sender, EventArgs e)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(@"http://localhost:1561/Default.aspx");
request.CookieContainer = cc;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string str2 = request.CookieContainer.GetCookieHeader(new Uri(@"http://localhost:1561/A/B/C"));
}
后发现我设置的cookie会丢失掉,如果设置cookie的domain也会发生这种情况。
我现在想取到不同路径和domain下的该如何处理哦
2、Cookie超时
3、Cookie中含有一些非法字符,致使浏览器丢弃Cookie
4、程序源码可能有多处重复设置或取消Cookie---
Domain的值设置为 ".hiscr.cn"时,表示hiscr.cn下的所有二级目录(注意最前面的一个点号)。
比如:"bbs.hiscr.cn" "www.hiscr.cn"等等都是该域下的子域。
否则需要指定具体的域名,比如"www.hiscr.cn".