清除 WebRequest 中的 cookie 容器

发布于 2024-08-28 11:50:53 字数 2207 浏览 11 评论 0原文

我使用 WebRequest 对象将数据发布到登录页面,然后将数据发布到同一站点上的单独页面。我正在实例化 CookieContainer 并将其分配给 WebRequest 对象,以便处理 cookie。问题是我不想在将数据发布到其他页面后保留 cookie。我怎样才能删除那个cookie?

private CookieContainer cookie_m;
protected CookieContainer CookieContainer
{
    get
    {
        if (cookie_m == null)
        {
            cookie_m = new CookieContainer();
        }
        return cookie_m;
    }
    set
    {
        cookie_m = value;
    }
}

protected virtual void SetData(WebRequest request, string sData)
{
    if (!String.IsNullOrEmpty(sData))
    {
        byte[] binPostData = System.Text.Encoding.ASCII.GetBytes(sData);
        request.ContentLength = binPostData.Length;
        System.IO.Stream sRequest = request.GetRequestStream();
        try
        {
            sRequest.Write(binPostData, 0, binPostData.Length);
        }
        finally
        {
            sRequest.Close();
        }
    }
}

private HttpWebRequest GetNewRequest(string sUrl)
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sUrl);
    request.CookieContainer = this.CookieContainer;
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";

    return request;
}

public override void Submit()
{
    //Login 
    HttpWebRequest request = GetNewRequest("http://mytest/login.asp");

    base.SetData(request, "action=validate_login&login=test&password=test");
    WebResponse response = request.GetResponse();

    System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream());
    string sResponse = sr.ReadToEnd();

    //Entry screen
    request = GetNewRequest("http://mytest/CustCreate.asp");
    base.SetData(request, "Site=xyz&Cust=test");
    response = request.GetResponse();

    sr = new System.IO.StreamReader(response.GetResponseStream());
    sResponse = sr.ReadToEnd();

    //Sutmit
    request = request = GetNewRequest("http://mytest/CustCreate.asp");
    base.SetData(request, "Site=xyz&mydatahere&B1=Submit");
    response = request.GetResponse();

    sr = new System.IO.StreamReader(response.GetResponseStream());
    sResponse = sr.ReadToEnd();

    //How to delete cookies that have been saved?
}

I'm using the WebRequest object to post data to a login page, then post data to a seperate page on the same site. I am instantiating a CookieContainer and assigning it to the WebRequest object so that the cookies are handled. The problem is that I do not want to retain the cookie after I post data to the other page. How can I delete that cookie?

private CookieContainer cookie_m;
protected CookieContainer CookieContainer
{
    get
    {
        if (cookie_m == null)
        {
            cookie_m = new CookieContainer();
        }
        return cookie_m;
    }
    set
    {
        cookie_m = value;
    }
}

protected virtual void SetData(WebRequest request, string sData)
{
    if (!String.IsNullOrEmpty(sData))
    {
        byte[] binPostData = System.Text.Encoding.ASCII.GetBytes(sData);
        request.ContentLength = binPostData.Length;
        System.IO.Stream sRequest = request.GetRequestStream();
        try
        {
            sRequest.Write(binPostData, 0, binPostData.Length);
        }
        finally
        {
            sRequest.Close();
        }
    }
}

private HttpWebRequest GetNewRequest(string sUrl)
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sUrl);
    request.CookieContainer = this.CookieContainer;
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";

    return request;
}

public override void Submit()
{
    //Login 
    HttpWebRequest request = GetNewRequest("http://mytest/login.asp");

    base.SetData(request, "action=validate_login&login=test&password=test");
    WebResponse response = request.GetResponse();

    System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream());
    string sResponse = sr.ReadToEnd();

    //Entry screen
    request = GetNewRequest("http://mytest/CustCreate.asp");
    base.SetData(request, "Site=xyz&Cust=test");
    response = request.GetResponse();

    sr = new System.IO.StreamReader(response.GetResponseStream());
    sResponse = sr.ReadToEnd();

    //Sutmit
    request = request = GetNewRequest("http://mytest/CustCreate.asp");
    base.SetData(request, "Site=xyz&mydatahere&B1=Submit");
    response = request.GetResponse();

    sr = new System.IO.StreamReader(response.GetResponseStream());
    sResponse = sr.ReadToEnd();

    //How to delete cookies that have been saved?
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

梦行七里 2024-09-04 11:50:53

要删除 cookie,您需要将其过期日期设置为过去的日期。这告诉浏览器它已过期,浏览器将删除它。

下面是 来自 msdn 的示例,说明如何在 C# 中执行此操作(不是确定您使用的是哪种语言)。

if (Request.Cookies["UserSettings"] != null)
{
    HttpCookie myCookie = new HttpCookie("UserSettings");
    myCookie.Expires = DateTime.Now.AddDays(-1d);
    Response.Cookies.Add(myCookie);
}

To delete a cookie, you need to set the expiration date on it to a date in the past. This tells the browser it's expired and the browser will delete it.

Here's an example from msdn on how to do this in C# (not sure which language you're using).

if (Request.Cookies["UserSettings"] != null)
{
    HttpCookie myCookie = new HttpCookie("UserSettings");
    myCookie.Expires = DateTime.Now.AddDays(-1d);
    Response.Cookies.Add(myCookie);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文