简体   繁体   English

使用Windows Forms C#Application创建cookie?

[英]Create a cookie using Windows Forms C# Application?

I am writing an extension for IE using C# and Windows Forms / BHO Objects. 我正在使用C#和Windows Forms / BHO对象编写IE的扩展。 I need to create a cookie which I can use to access information from other classes / forms in the app. 我需要创建一个cookie,我可以使用它来访问应用程序中其他类/表单的信息。

The various methods for creating cookies all seem to be related to ASP.NET and using the Http Response Filter, which do not help me here. 创建cookie的各种方法似乎都与ASP.NET和使用Http响应过滤器有关,这对我没有帮助。 I am not sure that I even need a session for what I am trying to accomplish, I just need to be able to create the cookie, access the information from the application, and then purge when needed. 我不确定我是否需要一个会话来完成我想要完成的任务,我只需要能够创建cookie,从应用程序访问信息,然后在需要时清除。 Thanks! 谢谢!

Sessions and cookies are web technology. 会话和cookie是Web技术。 You probably won't need to have that in your Windows application. 您可能不需要在Windows应用程序中使用它。 Instead you can use a static class. 相反,您可以使用静态类。

internal static class MySharedInfo {
    /* static proerties and whatever you need */
}

This is just one way. 这只是一种方式。 A really suitable method depends on what you actually want to achieve. 一个非常合适的方法取决于您实际想要实现的目标。

I also used similar code for our application cookies. 我还为我们的应用程序cookie使用了类似的代码。

 HttpWebRequest runTest;
 //...do login request
//get cookies from response

CookieContainer myContainer = new CookieContainer();
for (int i = 0; i < Response.Cookies.Count; i++)
{
 HttpCookie http_cookie = Request.Cookies[i];
 Cookie cookie = new Cookie(http_cookie.Name, http_cookie.Value, http_cookie.Path);
 myContainer.Add(new Uri(Request.Url.ToString()), cookie);
}

//later:
 HttpWebRequest request =       
(HttpWebRequest)WebRequest.Create("http://www.url.com/foobar");
 request.CookieContainer = myContainer;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM