【问题标题】:VB.Net HttpWebRequest - Transfer cookies from HttpWebRequest to WebBrowserVB.Net HttpWebRequest - 将 cookie 从 HttpWebRequest 传输到 WebBrowser
【发布时间】:2017-05-11 06:57:04
【问题描述】:

我想将 Cookie 从 HttpWebRequest 传输到 WebBrowser。 我尝试了下面的代码,但没有工作。

我来自 HttpWebRequest 的 cookie 被命名为:loginPostcookie

InternetSetCookieEx的参数我设置的很好吗?

<DllImport("wininet.dll", SetLastError:=True)> _
    Public Shared Function InternetSetCookieEx(url As String, cookieName As String, cookieData As StringBuilder, dwFlags As Int32, lpReserved As IntPtr) As Boolean
    End Function
    
    
    InternetSetCookieEx("https://www.facebook.com/", loginPostcookie.ToString, "TestData=Test;", 0, 0)
    
        Me.WebBrowser1.Navigate("https://www.facebook.com/")
        
     

【问题讨论】:

标签: vb.net


【解决方案1】:

我使用 cookie 允许我的用户将他们的用户名保存在 cookie 中,这样他们在登录时就不必继续输入。我使用下面的代码设置和检索 cookie"

' Set the cookie when the user checks the checkbox
If CheckBox_SaveUserName.Checked Then
    ' Save whatever username is in the textbox into the cookie
    Dim oCookie As HttpCookie = Request.Cookies("SignIn")
    oCookie = New HttpCookie("SignIn", txtUserName.Text)
    oCookie.Expires = DateTime.Now.AddYears(100)
    Response.AppendCookie(oCookie)
    'Response.Cookies("SignIn")("User") = txtUsername.Text
 Else
     ' To delete the cookie, we expire it.  Browser security won't let use remove it.
     Response.Cookies("SignIn").Expires = DateTime.Now.AddYears(-50)
 End If

当用户访问登录页面时,我会检查 cookie:

' If the signin cookie exists, get the username and check the checkbox
If Not IsNothing(Request.Cookies("SignIn")) Then
    If txtUserName.Text & "" = "" Then
        txtUserName.Text = Request.Cookies("SignIn").Value
        CheckBox_SaveUserName.Checked = True
    End If
End If

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-04
    • 2013-04-29
    • 2014-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多