2

所以我已经发送了将近两个小时来寻找答案,但没有任何效果。我需要通过我的 webbrowser 对象发送一些 cookie,但由于某种原因,我的 PHP 文件没有读取这些 cookie:

<?php die('Your username is '.$_COOKIE['user']); ?>

我的 VB 代码发送 cookie:

For i = 0 To 4
uploadBoxes(i).Navigate("about:blank")
uploadBoxes(i).Document.Cookie = "user=" & username.Text
uploadBoxes(i).Navigate("http://*****/uploader/app.php")
Next i

同样,任何帮助将不胜感激,是的,我确实需要通过 webbrowser 对象发送它。我还浏览了 MSDN 数据库,甚至对这个问题也没有多少启发。

- - - - - - - - - - - - - - - - - - - - - 答案 - - - ------------------------------------------

因此,我采用了 InternetSetCookie 方法,并提出了用于制作 cookie 的代码:

Imports System.Runtime.InteropServices

   ' 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 sub something()
              Dim bRet As Boolean
                bRet = InternetSetCookie("http://*****/uploader/app.php", _
                 "user", "admin")
                If bRet = False Then
                    MsgBox("Failed")
                End If
                uploadBoxes(i).Navigate("http:/*****/uploader/app.php")
End sub
4

2 回答 2

3

http://pinvoke.net/default.aspx/wininet.InternetSetCookie

  <DllImport("wininet.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
      Public Shared Function InternetSetCookie(lpszUrl As String, _
      lpszCookieName As String, lpszCookieData As String) As Boolean
     End Function

Imports System.Runtime.InteropServices
Sub InternetSetCookiePseudoCode()
'`CookieCollection` was populated using HttpWebRequest/Response calls
Dim i As Integer = 0
InternetSetCookie("https://www.url.com/", Nothing, CookieCollection(i).ToString() & "; expires = Sun, 01-Jul-2014 00:00:00 GMT")
'repeat for however many cookies you've got

browser.Navigate("https://www.url.com/", True)
End Sub
于 2012-07-31T01:45:38.613 回答
0

这是我用来为 VB.Net Webbrowser Control 手动设置 cookie 的一些旧版本:

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

[...]

Dim CookieVal As String = "asdfghjkl12345"
Dim CookieNamestr As String = "sessionid"
Try
    Dim Result As Long = InternetSetCookie("https://www.url.com/", CookieNamestr, CookieVal )
Catch ex As Exception
End Try
于 2016-06-15T13:23:06.900 回答