C# HttpWebRequest CookieContainer/Collection 在对象实例之间持久存在?

发布于 2024-10-24 08:18:22 字数 1811 浏览 5 评论 0原文

我的登录程序遇到了问题。如果我登录一次,然后将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坠似风落 2024-10-31 08:18:22

当您使用完 cookie 后,使其过期,它就会消失。 (又名,将其到期日期设置为经过的时间)。

Expire the cookie when you are done with it and it will go away. ( AKA, set its expires date to an elapsed time ).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文