C# HttpWebRequest CookieContainer/Collection 在对象实例之间持久存在?
我的登录程序遇到了问题。如果我登录一次,然后将 CookieContainer 和 CookieCollection (以及我的 http 类)设置为 null,然后尝试再次登录,它仍然会提交第一个请求中的 cookie。为什么饼干会留下来?
示例:
HTTP uh;
MainWindow()
{
uh = new HTTP();
uh.login("mySite");
//Logging in....
//Login Successful.....
uh.cookieContainer = null;
uh.cookieCollection = null;
uh = null;
uh = new HTTP();
uh.loging("mySite");
//Logging in, with cookies from first login
//Login Fails.....
}
编辑: HTTP 类的粗略表示...
public class HTTP()
{
public CookieContainer cookieContainer;
public CookieCollection cookieCollection;
private HttpWebRequest Request;
private HttpWebResponse Response;
public HTTP()
{
this.cookieContainer = new CookieContainer();
this.cookieCollection = new CookieCollection();
}
public HttpWebResponse login(string url)
{
string[] cred = new string[2] { "un", "pw" };
this.Request = (HttpWebRequest)HttpWebRequest.Create(url);
this.Request.CookieContainer = cookieContainer;
byte[] ByteArray = Encoding.UTF8.GetBytes(String.Format("un={0}&pw={1}", cred));
this.Request.ContentLength = ByteArray.Length;
Stream DataStream = this.Request.GetRequestStream();
DataStream.Write(ByteArray, 0, ByteArray.Length);
DataStream.Close();
Response = (HttpWebResponse)this.Request.GetResponse();
this.cookieCollection = Response.Cookies;
return Response;
}
public bool responseHandle(HttpWebResponse r)
{
//Determines success, logs headers, html body, etc..
}
}
编辑 |解决方案: 上面的任何代码都没有问题。我犯了一个愚蠢的错误,没有将 HTTP
null/new 代码放入我的注销按钮中。所以它从未重置。抱歉浪费了大家的时间。
I've run into a problem with my Login program. If I login one time, then null CookieContainer and CookieCollection (along with my http class), then try to login again, it still submits the cookies from the first request. Why to the cookies stick around?
Example:
HTTP uh;
MainWindow()
{
uh = new HTTP();
uh.login("mySite");
//Logging in....
//Login Successful.....
uh.cookieContainer = null;
uh.cookieCollection = null;
uh = null;
uh = new HTTP();
uh.loging("mySite");
//Logging in, with cookies from first login
//Login Fails.....
}
EDIT: Rough Representation of the HTTP class...
public class HTTP()
{
public CookieContainer cookieContainer;
public CookieCollection cookieCollection;
private HttpWebRequest Request;
private HttpWebResponse Response;
public HTTP()
{
this.cookieContainer = new CookieContainer();
this.cookieCollection = new CookieCollection();
}
public HttpWebResponse login(string url)
{
string[] cred = new string[2] { "un", "pw" };
this.Request = (HttpWebRequest)HttpWebRequest.Create(url);
this.Request.CookieContainer = cookieContainer;
byte[] ByteArray = Encoding.UTF8.GetBytes(String.Format("un={0}&pw={1}", cred));
this.Request.ContentLength = ByteArray.Length;
Stream DataStream = this.Request.GetRequestStream();
DataStream.Write(ByteArray, 0, ByteArray.Length);
DataStream.Close();
Response = (HttpWebResponse)this.Request.GetResponse();
this.cookieCollection = Response.Cookies;
return Response;
}
public bool responseHandle(HttpWebResponse r)
{
//Determines success, logs headers, html body, etc..
}
}
EDIT | SOLUTION:
There was no problem with the any code above. I made the dumb mistake of not putting the HTTP
null/new code in my logout button. So it was never reseting. Sorry for wasting everyones time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您使用完 cookie 后,使其过期,它就会消失。 (又名,将其到期日期设置为经过的时间)。
Expire the cookie when you are done with it and it will go away. ( AKA, set its expires date to an elapsed time ).