Omar J. Omar J. - 5 months ago 26
Vb.net Question

Send Cookie through WebBrowser in VB

So I have sent close to two hours searching for an answer but nothing is working. I need to to send a few cookies through my webbrowser object but for some reason the cookies aren't being read by my PHP file:

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


And my VB code to send the cookies:

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


Again, any help would be appreciated and yes, I do need to send it over the webbrowser object. I also have browsed through MSDN database and even that has shed little light on this problem.

------------------------------------------ THE ANSWER -------------------------------------------------

So I took the InternetSetCookie method and came up with this code that worked on making the cookies:

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

Answer

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
Comments