VB通过webbrowser获取httponly的cookie

大家都知道webbrowser是直接可以获取到cookie的。

可以通过webbrowser1.document.cookie来获取,但是利用这种方法!是获取不到httponly的cookie的!

相信各位百度到这篇文章,也是同样遇到小编遇到的问题了!

小编找了很久,,在网上找到以下解决方法

出处:http://www.cnblogs.com/nanfei/p/4106498.html

Option Explicit

' No more data is available.
Const ERROR_NO_MORE_ITEMS = 259

' The data area passed to a system call is too small.
Const ERROR_INSUFFICIENT_BUFFER = 122

Private Declare Function InternetSetCookie Lib "wininet.dll" _
 Alias "InternetSetCookieA" _
 (ByVal lpszUrlName As String, _
 ByVal lpszCookieName As String, _
 ByVal lpszCookieData As String) As Boolean

Private Declare Function InternetGetCookie Lib "wininet.dll" _
 Alias "InternetGetCookieA" _
 (ByVal lpszUrlName As String, _
 ByVal lpszCookieName As String, _
 ByVal lpszCookieData As String, _
 lpdwSize As Long) As Boolean

Private Sub Command1_Click()
    Dim bRet As Boolean
    bRet = InternetSetCookie("http://xxxx/xxxx.htm", "Test", "Sent as Test via VB")
    If bRet = False Then
        MsgBox "Failed"
    End If
End Sub

Private Sub Command2_Click()
    Dim sCookieVal As String * 256
    Dim bRet As Boolean
    bRet = InternetGetCookie("http://wwww.baidu.com", "Test", sCookieVal, 255)
    If bRet = False Then
      MsgBox "Failed"
    Else
      Debug.Print sCookieVal
    End If
End Sub

Private Sub Form_Load()
    WebBrowser1.Navigate "http://www.baidu.com"
End Sub

经过测试,发现这种方法只能获取到普通cookie,一样获取不到标记为httponly的cookie

好了!重点重点来了!

既然通过API能获取到cookie 那是不是也能获取到httponly的呢?

参考了之前C#获取的方法!

找到以下函数可以解决!

'声明API
Private Const INTERNET_COOKIE_HTTPONLY As Integer = 8192
Private Declare Function InternetGetCookieEx Lib "wininet.dll" Alias "InternetGetCookieExA" (ByVal url As String, ByVal cookieName As String, ByVal cookieData As String, ByRef Size As Integer, ByVal Flags As Integer, ByVal pReserved As Long) As Boolean

不要问我8192是啥意思 我也是百度到的

'调用
Dim Buffer As String * 8096
If InternetGetCookieEx("http://www.baidu.com", vbNullString, Buffer, 8096, INTERNET_COOKIE_HTTPONLY, 0) Then
Msgbox(Buffer) '拿到的cookie
End If

这样就可以拿到httponly的cookie啦~!是不是很简单?


转载请注明出处~!




你可能感兴趣的:(VB)