如何在控制台应用程序中设置cookie并在IE11中打开URL? [英] How to set cookie from console app and open url in IE11?

查看:801
本文介绍了如何在控制台应用程序中设置cookie并在IE11中打开URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我尝试使用C#console应用程序在IE11上打开URL。没关系,但我还想在IE11上打开该URL之前设置cookie。我看到了这个 问题并完全按照答案给出



这里我的代码在main()内部

Hi,

I am try to open URL on IE11 using C# console application. It is OK but I also want to set the cookie before open that URL on IE11. I saw this question and do exactly as given in answer

here my code in inside main()

if (InternetSetCookie(webUrl, "mycookie1", "myCookieValue"))
{
    Console.WriteLine("1st Cookie is sucessfully created.");
}

if(IESetProtectedModeCookie(webUrl, "mycookie2", "data=mycookiedata; expires = " + DateTime.UtcNow.AddDays(1).ToString() + "; path=/", 0x10))
{
    Console.WriteLine("2nd Cookie is sucessfully created.");
}

Process.Start("IExplore.exe", webUrl);





两种方法都返回true并显示消息。但是当我尝试通过键入document.cookie使用开发人员工具控制台在IE11上查看cookie时,会出现未定义的消息。请帮助我。



both methods are return true and display the messages. But when I try to see the cookie on IE11 using developer tool console by typing document.cookie then undefined message occur. Please help me.

推荐答案

Cookies是PITA - Firefox或Chrome没有准备好IE浏览器; IE浏览器或Firefox无法读取Chrome文件; IE浏览器或Chrome浏览器无法读取和使用Firefox。



但这与您尝试做的无关。

如果你看一下 InternetSetCookie [< a href =https://msdn.microsoft.com/en-us/library/windows/desktop/aa385107(v=vs.85).aspx\"target =_ blanktitle =New Window> ^ ]它非常清楚地说:



没有过期日期的InternetSetCookie创建的Cookie存储在内存中,只能在创建的同一进程中使用包含截止日期的Cookie存储在windows\cookies目录中。



IESetProtectedModeCookie [ ^ ]如果非常特定日期时间格式它需要:



要创建持久性cookie,请在cookie数据中添加过期的名称/值对。值的格式必须为DAY,DD-MMM-YYYY HH:MM:SS GMT。 DAY是星期几的三个字母缩写,DD是月份的日期,MMM是月份的三个字母缩写,YYYY是年份,HH:MM:SS是一天中的时间24小时格式。



所以你的cookie需要一个到期日才能进入IE cookie缓存,因为IE将以不同的方式运行持续的那些需要一个非常具体的日期格式!



但是......如果你的用户没有安装IE(现在这很可能)你不能做任何可行的事情!
Cookies are a PITA - IE cookies aren't ready by Firefox, or Chrome; Chrome ones aren't read by IE or Firefox; and Firefox cookies aren't read by IE or Chrome.

But that is as nothing to what you are trying to do.
If you look at the documentation for InternetSetCookie[^] it says very clearly:

"Cookies created by InternetSetCookie without an expiration date are stored in memory and are available only in the same process that created them. Cookies that include an expiration date are stored in the windows\cookies directory."

And IESetProtectedModeCookie[^] if very sepecific about the date time format it requires:

"To create a persistent cookie, add an expires name/value pair to the cookie data. The format of the value must be DAY, DD-MMM-YYYY HH:MM:SS GMT. DAY is a three-letter abbreviation for the day of the week, DD is the day of the month, MMM is a three-letter abbreviation for the month, YYYY is the year, and HH:MM:SS is the time of day in 24-hour format."

So your cookies need an expiry date in order to go into the IE cookie cache, since IE will run in a different process. And the persistent ones need a very specific date format!

But...if your user doesn't have IE installed (and that's pretty likely these days) you can't do anything that will work!


这篇关于如何在控制台应用程序中设置cookie并在IE11中打开URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆