NotSupportedException was unhandled by user code exception

一世执手 提交于 2019-12-25 01:40:02

问题


I'am trying to store a value of Document.cookie into a string variable in my c# code. The idea here is to go through each tab in an Internet Explorer browser and then get the cookie information from the tab. So I have got the following,

ShellWindows iExplorerInstances = new ShellWindows();
                            bool found = false;
                            foreach (InternetExplorer iExplorer in       iExplorerInstances)
                        {
                            if (iExplorer.Name == "Internet Explorer")
                            {
                                string cookie = iExplorer.Document.cookie;

Now this works upon initial running of the code, but when it is run in the same session again it fails and hits NotSupportDeskException on the last line of code above, which is where the string cookie is declared and initialised (line 134). Is there a way around this?

The stack trace is as follows, at System.Dynamic.ComRuntimeHelpers.CheckThrowException(Int32 hresult, ExcepInfo& excepInfo, UInt32 argErr, String message) at CallSite.Target(Closure , CallSite , ComObject ) at CallSite.Target(Closure , CallSite , Object ) at hhsoutlookadin.ThisAddIn.d__3.MoveNext() in Somefile.cs:line 134. The message is "Exception from HRESULT: 0x800A01B6".


回答1:


I thought this be something to do with casting the Document.cookie object as a string, this appeared to be causing issues after running through the code once. So the Document object I now parse as a mshtml.IHTML2Document2 object. I then make a reference to it cookie object by storing it in a string, which works and doesnt cause any issues.

ShellWindows iExplorerInstances = new ShellWindows();
                        bool found = false;
                        foreach (InternetExplorer iExplorer in iExplorerInstances)
                        {
                            if (iExplorer.Name == "Internet Explorer")
                            {
                                string[] cookieCrumbs = { };
                                try
                                {
                                    mshtml.IHTMLDocument2 htmlDoc = iExplorer.Document as mshtml.IHTMLDocument2;
                                    string cookie = htmlDoc.cookie;


来源:https://stackoverflow.com/questions/28051279/notsupportedexception-was-unhandled-by-user-code-exception

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