所以我差不多花了两个小时寻找答案,但没有任何工作。我需要通过我的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
答案 0 :(得分:2)
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, lpszCookieDataAs 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
答案 1 :(得分: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