2012-07-30 55 views
1

所以我發送了近兩個小時尋找答案,但沒有任何工作。我要通過我的web瀏覽器對象發送一個幾塊餅乾,但由於某些原因,Cookie不會被我的PHP文件中讀取:在VB中通過WebBrowser發送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 

再次,任何幫助將不勝感激,是的,我需要通過網頁瀏覽器對象發送它。我也瀏覽了MSDN數據庫,甚至對這個問題也沒有多大的瞭解。

------------------------------------------ THE答案-------------------------------------------------

所以我把InternetSetCookie方法以及與此代碼上製作餅乾的工作上來:

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 

回答

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 
+0

感謝您的幫助傑里米,這是我一直在尋找對於。 – 2012-07-31 11:19:56

0

下面是一些老的版本我使用的VB.Net WebBrowser控件手動設置cookies:

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