【问题标题】:InternetExplorer.Application object and cookie containerInternetExplorer.Application 对象和 cookie 容器
【发布时间】:2010-05-19 09:47:46
【问题描述】:

我有以下用 VB.NET 编写的控制台应用程序:

Sub Main()
    Dim ie As Object = CreateObject("InternetExplorer.Application")
    ie.Visible = True
    ie.Navigate2("http://localhost:4631/Default.aspx")
End Sub

这个程序使用InternetExplorer.Application 自动化对象来启动一个 IE 窗口并导航给定的 url。我遇到的问题是,即使我启动了我的应用程序的多个实例,使用这种方法创建的 IE 窗口都共享同一个 cookie 容器。我可以使用任何参数来指定为每个窗口创建不同的 cookie 容器吗?

这是我用来测试cookies的网页:

<%@ Page Language="C#" AutoEventWireup="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        // Store something into the session in order to create the cookie
        Session["foo"] "bar";
        Response.Write(Session.SessionID);
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<body>
    <form id="form1" runat="server"></form>
</body>
</html>

【问题讨论】:

    标签: internet-explorer cookies automation


    【解决方案1】:

    关于CreateObject("InternetExplorer.Application"),您创建了一个Internet Explorer 实例,您的所有程序实例都通过这一进程进行通信。 每个进程都会保留 Cookies

    您可以尝试在您的应用程序中使用WebBrowser 控件代替(请参阅http://msdn.microsoft.com/en-us/library/3s8ys666.aspx)。您可以在http://msdn.microsoft.com/en-us/library/aa752044(VS.85).aspx 中找到比较两种方式的信息。如果您将在您的应用程序中使用WebBrowser 控件,则您的应用程序的所有实例都将拥有自己的一组cookie,但每个进程只有一组cookie,与您应用程序中WebBrowser 控件的数量无关。

    在任何进程中,您都可以随时清除关于后续调用的 cookie

    InternetSetOption(IntPtr.Zero, INTERNET_OPTION_END_BROWSER_SESSION, IntPtr.Zero, 0);
    

    (请参阅http://support.microsoft.com/kb/195192/en)再次展示了 cookie 持有的性质。

    【讨论】:

      猜你喜欢
      • 2017-05-24
      • 2017-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多