C#窗口应用程序中的Cookie [英] Cookies in c# window application
本文介绍了C#窗口应用程序中的Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
谁能举一个例子说明如何在 c#Windows应用程序中读取和写入cookie .
plz帮帮我..
亲爱的人们...我已经尝试过类似的事情..
我已经使用HttpWebResponse
添加了cookie.
can anyone give an example of how to read and write cookies in c# windows application.
plz help me..
dear folks... I have tried something like this..
I have added cookie using HttpWebResponse
.
private void btnResponse_Click(object sender, EventArgs e)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://192.168.1.100/login/index.aspx");
HttpWebResponse httpResponse = (HttpWebResponse)request.GetResponse();
Cookie ckepath = new Cookie("cielopath", "c:");
httpResponse.Cookies.Add(ckepath);
}
but when i request(try to get the value)it its empty..i think its not added in system....plz help
private void btnRequest_Click(object sender, EventArgs e)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://192.168.1.100/login/index.aspx");
HttpWebResponse httpResponse = (HttpWebResponse)request.GetResponse();
foreach (Cookie cke in httpResponse.Cookies)
{
string val = cke.ToString();
}
}
推荐答案
嗨Rahul,
AFAIK,Cookie适用于浏览器.如果您在Windows窗体中以任何方式使用Web浏览器控件,我认为您可以借助该控件来完成此操作,但是如果您在没有任何Web浏览器控件的情况下使用Windows窗体应用程序,则可以您无法使用这种Cookie的东西.如果您希望持久保存键/值,请尝试以纯文本或加密文本格式存储在某个平面文件中;否则,请尝试使用变量.
希望对您有所帮助!
快乐编码:)
Hi Rahul,
Well AFAIK, cookies are for browsers. If you''re using a web Browser control by any means inside your windows form, I think you may do it with the help of that control, but if you''re on to a windows form application without any web browser control then there''s no such cookie thing you can use. Try to store the keys/values in a plain or encrypted text form in some flat file if you want it to be persisted or just go with variables if you don''t.
Hope it helps !
Happy Coding :)
Cookie在Windows应用程序中不存在-而是将数据保存在文件,应用程序设置或注册表中,具体取决于数据量及其大小.的组织.您可以将其保存在数据库或XML文件中,也可以保存为striaght二进制信息.
但是,您不要在Windows应用程序中使用Cookie-它们是用于从服务器在客户端上进行设置的,并且这些概念在使用Windows应用程序时不适用.
应用程序设置可能是最简单的:
1)在解决方案资源管理器中单击项目,选择属性".
2)双击设置.设置".
3)在出现的对话框中,通过指定名称,类型和值来创建应用程序设置-将范围"保留为用户".
4)现在,您可以轻松保存和加载值:
Cookies do not exist in windows applications - instead data is either saved in files, or in application settings, or in the registry depending on the quantity of data and it''s organisation. You might save it in a database, or an XML file, or as striaght binary info.
But you don''t use cookies in windows apps - they are for setting on the client from the server and those concepts do not apply when using Windows apps.
Application settings are probably the easiest:
1) Click on the project in Solution Explorer, choose "Properties".
2) Double click "Settings.Settings".
3) In the resulting dialog, Create application settings by specifying the Name, Type and Value - leave the "Scope" as "User".
4) You can now save and load your values easily:
Properties.Settings.Default.MySettingString = "New Value";
Properties.Settings.Default.Save();
...
string myValue = Properties.Settings.Default.MySettingString;
这篇关于C#窗口应用程序中的Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文