我的登录程序遇到了问题。如果我登录一次,那么null CookieContainer和CookieCollection(以及我的http类),然后尝试再次登录,它仍然从第一个请求提交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代码放在我的注销按钮中。所以它永远不会重置。对不起浪费每个人的时间。
答案 0 :(得分:2)
当你完成cookie后它会过期,它会消失。 (AKA,将其到期日设置为已用时间)。