WebBrowser控件和Cookie [英] WebBrowser control and cookies

查看:179
本文介绍了WebBrowser控件和Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WebBrowser控件和cookie的问题。



首先,在一个普通的Web浏览器(我的情况下IE9) p>

1.1。我打开网页 http://www.gmail.com



我输入了我的用户名/密码,

我保留复选框保持登录取消选中,然后点击 ,

IE9打开我的Gmail页面,其中列出了所有邮件。没关系。



1.2。在Gmail页面顶部有很多链接,如日历,文档等。



当我点击文档链接,在IE9中的单独选项卡中打开。不会询问名称/ psw的其他登录信息。



现在,当我重复所有在WebBrowser控制(我已经创建了一个非常简单的VB.NET应用程序与单个WebBrowser控制) 。



2.1。在窗体加载事件中,执行以下代码:




Private Sub MyForm_Load(sender As System.Object,e As System.EventArgs )

     Me.MyWebBrowser.Navigate(http://www.gmail.com)

End Sub



2.2。我在WebBrowser控件中输入我的GMail登录信息(名称和psw),



2.3。当我点击文档链接时,会打开一个新的IE9实例,



2.4。 Google要求我在IE9窗口中重新登录,而不是显示我的文档列表。为什么?为什么我必须再次输入我的凭据?



我认为cookie的错误,它们在步骤2.2中设置不正确。



提前感谢,

Sal

您是否有任何想法,

其他信息:



我有我的WebBrowser NewWindow事件函数实现为:

  Private Sub MyWebBrowser_NewWindow(sender As System.Object,e As System.ComponentModel.CancelEventArgs)句柄MyWebBrowser.NewWindow 
Dim CookiesArr As String()= MyWebBrowser.Document。 Cookie.Split(;)
对于CookiesArr中的每个Cookie
Dim Idx As Long = Cookie.IndexOf(=)
如果Idx<> -1 Then
Dim CookieName As String = Cookie.Substring(0,Idx).Trim
Dim CookieValue As String = Cookie.Substring(Idx + 1).Trim
InternetSetCookie(http: //www.google.com,Nothing,CookieName +=+ CookieValue +; expires = Sat,05-Jan-2013 00:00:00 GMT)
End If
Next
End Sub



我相信InternetSetCookie()方法应该永久存储在C: \Users\Administrator\AppData\Roaming\Microsoft\Windows\Cookies目录以便在需要授权的Google网页打开时重复使用。

解决方案

这是因为网络浏览器控件是在单独的IE9窗口中打开你的链接,对吧?如果您在Winforms程序中的另一个Web浏览器控制窗口中打开它,或者在单击该链接的同一窗口中打开它,那么它应该能正常工作。



他们在会话cookie(在内存中)使用保存您的登录信息,而不是写入硬盘驱动器的排序,这就是为什么当您运行另一个进程时,信息(在内存会话cookie)不存在或传播到新进程。



因此,要拦截链接点击并在您选择的wb窗口中打开它,您需要拦截newwindow事件,取消导航和重新导航使用。导航到您选择的wb,如果你需要帮助,让我知道,但有很多在线。



此外,请记住,Web浏览器控件使用IE7默认情况下,即使您安装了IE9,也可以通过注册表更改。


I have a problem with WebBrowser control and cookies.

First of all what happens when one navigates in a normal Web browser (IE9 in my case):

1.1. I open Web page http://www.gmail.com.

I enter my username / password,
I leave the checkbox "Stay signed in" unchecked and click "Sign in",
IE9 opens my GMail page with all my mails listed. That is OK.

1.2. At the top of GMail page there are a lot of links like "Calendar", "Documents", etc.

When I click the "Documents" link, my documents page is opened in a separate tab in IE9. No additional login information as name / psw is asked. This is fine too.

Now, what happens when I repeat all that in WebBrowser control (I have created a very simple VB.NET application with single WebBrowser control in it).

2.1. In form load event the following code is executed:

Private Sub MyForm_Load(sender As System.Object, e As System.EventArgs)
    Me.MyWebBrowser.Navigate("http://www.gmail.com")
End Sub

2.2. I enter my GMail login information (name and psw) in WebBrowser control,

2.3. When I click the "Documents" link, a new instance of IE9 is opened,

2.4. Instead of showing a list of my documents, Google asks me to login again in an IE9 window. Why? Why I have to enter my credentials again?

I think there is something wrong with cookies and they are not set correctly in a step 2.2. Do you have any ideas what is wrong and what must be done to get cookies set correctly?

Thanks in advance,
Sal

Additional info:

I have my WebBrowser NewWindow event function implemented as:

Private Sub MyWebBrowser_NewWindow(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles MyWebBrowser.NewWindow
    Dim CookiesArr As String() = MyWebBrowser.Document.Cookie.Split(";")
    For Each Cookie In CookiesArr
        Dim Idx As Long = Cookie.IndexOf("=")
        If Idx <> -1 Then
            Dim CookieName As String = Cookie.Substring(0, Idx).Trim
            Dim CookieValue As String = Cookie.Substring(Idx + 1).Trim
            InternetSetCookie("http://www.google.com", Nothing, CookieName + " = " + CookieValue + "; expires = Sat,05-Jan-2013 00:00:00 GMT")
        End If
    Next
End Sub

I believe that InternetSetCookie() method should store cookies permanently in in "C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Cookies" directory for reuse when Google page, requiring authorization, is opened.

解决方案

that is because the web browser control is opening your link in a separate IE9 window, right? If you open it or have it opened in another web browser control window in your Winforms program, or in the same window where you clicked the link, then it should work properly.

They are using in session cookies (in memory) to hold your login information, not the sort written to the hard drive, this is why when you run another process, the information (in memory session cookies) are not present or propagated to the new process.

So, to intercept link clicking and have it open in a wb window of your choice, you need to intercept the newwindow event, cancel navigation and renavigate using .navigate to the wb of your choice, if you need help with this let me know but there are plenty online.

Also, keep in mind that the web browser control uses IE7 by default, even though you have IE9 installed, this can be changed via the registry.

这篇关于WebBrowser控件和Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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