NotSupportedException未被用户代码异常处理

时间:2015-01-20 17:13:48

标签: c# string cookies domdocument notsupportedexception

我正在尝试将Document.cookie的值存储到我的c#代码中的字符串变量中。这里的想法是浏览Internet Explorer浏览器中的每个选项卡,然后从选项卡中获取cookie信息。所以我得到了以下内容,

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

现在这可以在初始运行代码时运行,但是当它再次在同一个会话中运行时,它会失败并在上面的最后一行代码中触及NotSupportDeskException,这是声明和初始化字符串cookie的地方(第134行) 。有办法解决这个问题吗?

堆栈跟踪如下,    在System.Dynamic.ComRuntimeHelpers.CheckThrowException(Int32 hresult,ExcepInfo& excepInfo,UInt32 argErr,String message)    在CallSite.Target(Closure,CallSite,ComObject)    在CallSite.Target(Closure,CallSite,Object)    athhsoutlookadin.ThisAddIn.d__3.MoveNext()in Somefile.cs:第134行。 消息是"来自HRESULT的异常:0x800A01B6"。

1 个答案:

答案 0 :(得分:1)

我认为这与将Document.cookie对象转换为字符串有关,这似乎在运行代码之后导致问题。所以我现在将Document对象解析为mshtml.IHTML2Document2对象。然后我通过将它存储在一个字符串中来引用它的cookie对象,该字符串有效并且不会引起任何问题。

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;