How can I use ASP.NET to check if cookies are enabled without having a web page?

懵懂的女人 提交于 2019-12-25 18:04:45

问题


I am working on a desktop application that automates numerous websites and processes the returned data. This application will be run on a server and the user will connect with remote desktop. The automation breaks when the server doesn't have cookies enabled, and instead of having the application stop working, I would like to check the server settings and tell the user why it isn't working.

To do this, I decided to create an ASP.NET Web Service. I figured that the web service could store the cookie and then try to access the data. If the data is found, cookies are enabled and everything is good to go.

I have read numerous tutorials like: http://www.csharphelp.com/2006/05/c-and-cookies/ that explain how to store and access cookies through ASP.NET. All of these however use a website. The user clicks a button to generate a request and the web service can then send a response. I do not know how to make the process work without having a website, and I do not wish to automate a site of my own hosting as well. I feel like I should be able to run a method on my webservice and it should work.

Does anyone know how this would be done? I'd like a webservice that can handle request and response so I can create, store, and access cookies. I thank you for any help that you may offer.

Nathan Tornquist

EDIT:

I am working on an database driven application. Some of the pieces are automated web sites, where the user puts in some data and then the program opens up a webBrowser control, fills in the forms, and then formats the resulting data. I would like to test if cookies are enabled based on the users security privileges so that I can disable automation and allow the user to fill in the data on their own if required. Most of the websites require cookies to log in.

This will be running on various versions of Windows Server. By default I think the webBrowser control has the same security settings as Internet Explorer.


回答1:


You can verify those keys on registry:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Zones
HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Zones

Under those keys, you may see some keys like 0, 1, 2, 3 ... Each one is a Zone configuration.

Search for those entries in each Zone:

1A02
1A03
1A05
1A06

So you have to verify if those entries values are equals to 0.

See those links:

ms kb
question




回答2:


var request = (HttpWebRequest)WebRequest.Create("http://stackoverflow.com/");
request.Method =  "Head";
var response = (HttpWebResponse)request.GetResponse();
var usesCookies = !String.IsNullOrEmpty(response.Headers["Set-Cookie"]);


来源:https://stackoverflow.com/questions/6599195/how-can-i-use-asp-net-to-check-if-cookies-are-enabled-without-having-a-web-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!