Set and Get Cookies for a URL Using WinInet API PRODUCT :Microsoft Visual Basic for Windows PROD/VER:WINDOWS:5.0,6.0; OPER/SYS: KEYWORDS:kbVBp500 kbVBp600 kbInternet kbAPI kbWinInet kbInetDev====================================================================== --------------------------------------------------------------------- The information in this article applies to:- Microsoft Visual Basic Professional and Enterprise Editions for Windows, versions 5.0, 6.0 ---------------------------------------------------------------------SUMMARY =======From a Visual Basic application, you can set and get cookies on a client that corresponds to a given URL by using the InternetSetCookie and InternetGetCookie APIs from the WinInet.dll.MORE INFORMATION ================1. Create a new standard .exe project in Visual Basic. Form1 is created by default.2. Add the following controls to Form1:Control Name Caption -------------------------------------------------- Command Button Command1 InternetSetCookie Command Button Command2 InternetGetCookie3. In Form1, add the following code in the code window: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 = 122Private Declare Function InternetSetCookie Lib "wininet.dll" _ Alias "InternetSetCookieA" _ (ByVal lpszUrlName As String, _ ByVal lpszCookieName As String, _ ByVal lpszCookieData As String) As BooleanPrivate Declare Function InternetGetCookie Lib "wininet.dll" _ Alias "InternetGetCookieA" _ (ByVal lpszUrlName As String, _ ByVal lpszCookieName As String, _ ByVal lpszCookieData As String, _ lpdwSize As Long) As BooleanPrivate 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 SubPrivate Sub Command2_Click() Dim sCookieVal As String * 256 Dim bRet As Boolean bRet = InternetGetCookie("http://xxxx/xxxx.htm", _ "Test", sCookieVal, 255) If bRet = False Then MsgBox "Failed" Else MsgBox sCookieVal End If End Sub
上面的老大的方法可以取得Cookie啦
Public Declare Function InternetSetCookie Lib "wininet.dll" Alias "InternetSetCookieA" _ (ByVal lpszUrlName As String, _ ByVal lpszCookieName As String, _ ByVal lpszCookieData As String) As BooleanPublic Declare Function InternetGetCookie Lib "wininet.dll" _ Alias "InternetGetCookieA" _ (ByVal lpszUrlName As String, _ ByVal lpszCookieName As String, _ ByVal lpszCookieData As String, _ lpdwSize As Long) As BooleanPublic Function SetCookie(ByVal UrlName As String, ByVal CookieName As String, ByVal CookieData As String) Dim blnReturn As Boolean blnReturn = InternetSetCookie(UrlName, CookieName, CookieData) If blnReturn = False Then MsgBox "Make Cookie Operation Failed!", vbCriticalEnd FunctionPublic Function GetCookie(ByVal UrlName As String, ByVal CookieName As String) As String Dim strCookieText As String * 256 Dim blnReturn As Boolean blnReturn = InternetGetCookie(UrlName, CookieName, strCookieText, 255) If blnReturn = False Then MsgBox "Get Cookie Operation Failed", vbCritical GetCookie = "Error" Else MsgBox strCookieText, vbInformation GetCookie = strCookieText End If End Function
PRODUCT :Microsoft Visual Basic for Windows
PROD/VER:WINDOWS:5.0,6.0;
OPER/SYS:
KEYWORDS:kbVBp500 kbVBp600 kbInternet kbAPI kbWinInet kbInetDev======================================================================
---------------------------------------------------------------------
The information in this article applies to:- Microsoft Visual Basic Professional and Enterprise Editions for
Windows, versions 5.0, 6.0
---------------------------------------------------------------------SUMMARY
=======From a Visual Basic application, you can set and get cookies on a client
that corresponds to a given URL by using the InternetSetCookie and
InternetGetCookie APIs from the WinInet.dll.MORE INFORMATION
================1. Create a new standard .exe project in Visual Basic. Form1 is created by
default.2. Add the following controls to Form1:Control Name Caption
--------------------------------------------------
Command Button Command1 InternetSetCookie
Command Button Command2 InternetGetCookie3. In Form1, add the following code in the code window: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 = 122Private Declare Function InternetSetCookie Lib "wininet.dll" _
Alias "InternetSetCookieA" _
(ByVal lpszUrlName As String, _
ByVal lpszCookieName As String, _
ByVal lpszCookieData As String) As BooleanPrivate Declare Function InternetGetCookie Lib "wininet.dll" _
Alias "InternetGetCookieA" _
(ByVal lpszUrlName As String, _
ByVal lpszCookieName As String, _
ByVal lpszCookieData As String, _
lpdwSize As Long) As BooleanPrivate 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 SubPrivate Sub Command2_Click()
Dim sCookieVal As String * 256
Dim bRet As Boolean
bRet = InternetGetCookie("http://xxxx/xxxx.htm", _
"Test", sCookieVal, 255)
If bRet = False Then
MsgBox "Failed"
Else
MsgBox sCookieVal
End If
End Sub
End Function