Author Topic: WININET Examples  (Read 10873 times)

0 Members and 1 Guest are viewing this topic.

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2483
  • User-Rate: +204/-0
WININET Examples
« on: August 29, 2011, 01:43:42 AM »
 
The Windows Internet (WinINet) application programming interface (API) enables applications to interact with FTP, and HTTP protocols to access Internet resources. As standards evolve, these functions handle the changes in underlying protocols, enabling them to maintain consistent behavior.

With a few exceptions, WinINet is a superset of WinHTTP. When selecting between the two, you should use WinINet, unless you plan to run within a service or service-like process that requires impersonation and session isolation.

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2483
  • User-Rate: +204/-0
WININET: InternetAttemptConnect Function
« Reply #1 on: August 29, 2011, 01:44:54 AM »
 
The following code attempts to make a connection to the Internet.

Code: [Select]
#COMPILE EXE
#DIM ALL
#INCLUDE "WININET.INC"

FUNCTION PBMAIN () AS LONG

   IF InternetAttemptConnect(0) = %ERROR_SUCCESS THEN
      ? "You can connect to the Internet"
   ELSE
      ? "You cannot connect to the Internet"
   END IF

   #IF %DEF(%PB_CC32)
      WAITKEY$
   #ENDIF

END FUNCTION

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2483
  • User-Rate: +204/-0
WININET: InternetCheckConnection Function
« Reply #2 on: August 29, 2011, 01:45:55 AM »
 
The following code checks if a connection to the Internet can be established.

Code: [Select]
#COMPILE EXE
#DIM ALL
#INCLUDE "WININET.INC"

FUNCTION PBMAIN () AS LONG

   IF InternetCheckConnection("http://www.jose.it-berater.org", _
      %FLAG_ICC_FORCE_CONNECTION, 0) = %ERROR_SUCCESS THEN
      ? "Connection failed!"
   ELSE
      ? "Connection succeeded!"
   END IF

   #IF %DEF(%PB_CC32)
      WAITKEY$
   #ENDIF

END FUNCTION

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2483
  • User-Rate: +204/-0
WININET: InternetCombineUrl Function
« Reply #3 on: August 29, 2011, 01:47:34 AM »
 
Combines a base and relative URL into a single URL.

Code: [Select]
#COMPILE EXE
#DIM ALL
#INCLUDE "WININET.INC"

FUNCTION PBMAIN () AS LONG

   LOCAL szUrl AS ASCIIZ * %INTERNET_MAX_PATH_LENGTH
   
   InternetCombineUrl "http://www.powerbasic.com", "/support/forums/Ultimate.cgi" , _
                      szUrl, SIZEOF(szUrl), %ICU_ENCODE_PERCENT

   ? szUrl

   #IF %DEF(%PB_CC32)
      WAITKEY$
   #ENDIF

END FUNCTION

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2483
  • User-Rate: +204/-0
WININET: InternetCanonicalizeUrl Function
« Reply #4 on: August 29, 2011, 01:48:27 AM »
 
The following example canonicalizes the specified url.

Code: [Select]
#COMPILE EXE
#DIM ALL
#INCLUDE "WININET.INC"

FUNCTION PBMAIN () AS LONG

   LOCAL szUrl AS ASCIIZ * %INTERNET_MAX_PATH_LENGTH
   LOCAL szBuffer AS ASCIIZ * %INTERNET_MAX_PATH_LENGTH

   szUrl = "http://msdn2.microsoft.com/en-us/ library/"
   InternetCanonicalizeUrl szUrl, szBuffer, SIZEOF(szBuffer), %ICU_BROWSER_MODE
   ? szBuffer

   #IF %DEF(%PB_CC32)
      WAITKEY$
   #ENDIF

END FUNCTION

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2483
  • User-Rate: +204/-0
WININET: InternetCrackUrl Function
« Reply #5 on: August 29, 2011, 01:49:15 AM »
 
The following code cracks a URL into its component parts.

Code: [Select]
' SED_PBCC - Use the PBCC compiler
#COMPILE EXE
#DIM ALL
#INCLUDE "WININET.INC"

FUNCTION PBMAIN () AS LONG

   LOCAL uc AS URL_COMPONENTS
   LOCAL szUrl AS ASCIIZ * %INTERNET_MAX_PATH_LENGTH
   LOCAL szDomain AS ASCIIZ * 256
   LOCAL szPath AS ASCIIZ * %INTERNET_MAX_PATH_LENGTH
   LOCAL szExtra AS ASCIIZ * 256

   uc.dwStructSize      = SIZEOF(uc)
   uc.lpszScheme        = %NULL
   uc.dwSchemeLength    = %NULL
   uc.lpszHostName      = VARPTR(szDomain)
   uc.dwHostNameLength  = SIZEOF(szDomain)
   uc.lpszUserName      = %NULL
   uc.dwUserNameLength  = %NULL
   uc.lpszPassword      = %NULL
   uc.dwPasswordLength  = %NULL
   uc.lpszUrlPath       = VARPTR(szPath)
   uc.dwUrlPathLength   = SIZEOF(szPath)
   uc.lpszExtraInfo     = VARPTR(szExtra)
   uc.dwExtraInfoLength = SIZEOF(szExtra)

   szUrl = "http://msdn2.microsoft.com/en-us/library/aa384376.aspx"
   InternetCrackUrl szUrl, SIZEOF(szUrl), %ICU_ESCAPE, uc

   PRINT "Host name: " uc.@lpszHostName
   PRINT "Path: " uc.@lpszUrlPath
   PRINT "Port number: " uc.nPort

   WAITKEY$

END FUNCTION

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2483
  • User-Rate: +204/-0
WININET: InternetGetConnectedState Function
« Reply #6 on: August 29, 2011, 01:50:05 AM »
 
The following code retrieves the connected state.

Code: [Select]
#COMPILE EXE
#DIM ALL
#INCLUDE "WININET.INC"

FUNCTION PBMAIN () AS LONG

   LOCAL dwFlags AS DWORD

   IF ISTRUE InternetGetConnectedState(dwFlags, 0) THEN
      ? "Connected"
   ELSE
      ? "Disconnected"
   END IF

   #IF %DEF(%PB_CC32)
      WAITKEY$
   #ENDIF

END FUNCTION

The following code retrieves the connected state and displays the connection description.

Code: [Select]
' SED_PBCC - Use the PBCC compiler
#COMPILE EXE
#DIM ALL
#INCLUDE "WININET.INC"

FUNCTION PBMAIN () AS LONG

   LOCAL dwFlags AS DWORD

   IF ISTRUE InternetGetConnectedState(dwFlags, 0) THEN
      PRINT "Connected"
   ELSE
      PRINT "Disconnected"
   END IF
   
   IF (dwFlags AND %INTERNET_CONNECTION_CONFIGURED) = %INTERNET_CONNECTION_CONFIGURED THEN
      PRINT "Local system has a valid connection to the Internet, but it may or may not be currently connected."
   END IF

   IF (dwFlags AND %INTERNET_CONNECTION_LAN) = %INTERNET_CONNECTION_LAN THEN
      PRINT "Local system uses a local area network to connect to the Internet."
   END IF

   IF (dwFlags AND %INTERNET_CONNECTION_MODEM) = %INTERNET_CONNECTION_MODEM THEN
      PRINT "Local system uses a modem to connect to the Internet."
   END IF

   IF (dwFlags AND %INTERNET_CONNECTION_OFFLINE) = %INTERNET_CONNECTION_OFFLINE THEN
      PRINT "Local system is in offline mode."
   END IF

   IF (dwFlags AND %INTERNET_CONNECTION_PROXY) = %INTERNET_CONNECTION_PROXY THEN
      PRINT "Local system uses a proxy server to connect to the Internet."
   END IF

   IF (dwFlags AND %INTERNET_RAS_INSTALLED) = %INTERNET_RAS_INSTALLED THEN
      PRINT "Local system has RAS installed."
   END IF

   WAITKEY$

END FUNCTION

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2483
  • User-Rate: +204/-0
WININET: InternetGetConnectedStateEx Function
« Reply #7 on: August 29, 2011, 01:50:59 AM »
 
The following code retrieves the connected state and the connection name.

Code: [Select]
' SED_PBCC - Use the PBCC compiler
#COMPILE EXE
#DIM ALL
#INCLUDE "WININET.INC"

FUNCTION PBMAIN () AS LONG

   LOCAL dwFlags AS DWORD
   LOCAL szConnectionName AS ASCIIZ * 256

   IF ISTRUE InternetGetConnectedStateEx(dwFlags, szConnectionName, SIZEOF(szConnectionName), 0) THEN
      PRINT "Connected"
      PRINT "Connection name: " szConnectionName
   ELSE
      PRINT "Disconnected"
   END IF
   
   IF (dwFlags AND %INTERNET_CONNECTION_CONFIGURED) = %INTERNET_CONNECTION_CONFIGURED THEN
      PRINT "Local system has a valid connection to the Internet, but it may or may not be currently connected."
   END IF

   IF (dwFlags AND %INTERNET_CONNECTION_LAN) = %INTERNET_CONNECTION_LAN THEN
      PRINT "Local system uses a local area network to connect to the Internet."
   END IF

   IF (dwFlags AND %INTERNET_CONNECTION_MODEM) = %INTERNET_CONNECTION_MODEM THEN
      PRINT "Local system uses a modem to connect to the Internet."
   END IF

   IF (dwFlags AND %INTERNET_CONNECTION_OFFLINE) = %INTERNET_CONNECTION_OFFLINE THEN
      PRINT "Local system is in offline mode."
   END IF

   IF (dwFlags AND %INTERNET_CONNECTION_PROXY) = %INTERNET_CONNECTION_PROXY THEN
      PRINT "Local system uses a proxy server to connect to the Internet."
   END IF

   IF (dwFlags AND %INTERNET_RAS_INSTALLED) = %INTERNET_RAS_INSTALLED THEN
      PRINT "Local system has RAS installed."
   END IF

   WAITKEY$

END FUNCTION

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2483
  • User-Rate: +204/-0
WININET: InternetGetCookie Function
« Reply #8 on: August 29, 2011, 01:51:50 AM »
 
The following code retrieves the cookie for the specified URL.

Code: [Select]
#COMPILE EXE
#DIM ALL
#INCLUDE "WININET.INC"

FUNCTION PBMAIN () AS LONG

   LOCAL hr AS LONG
   LOCAL dwSize AS DWORD
   LOCAL strCookieData AS STRING
     
   hr = InternetGetCookie("http://www.powerbasic.com/", "", BYVAL %NULL, dwSize)
   IF ISTRUE hr THEN
      strCookieData = SPACE$(dwSize)
      hr = InternetGetCookie("http://www.powerbasic.com/", "", BYVAL STRPTR(strCookieData), dwSize)
      ? strCookieData
   END IF
   
   #IF %DEF(%PB_CC32)
      WAITKEY$
   #ENDIF

END FUNCTION

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2483
  • User-Rate: +204/-0
WININET: InternetOpen Function
« Reply #9 on: August 29, 2011, 01:54:16 AM »
 
The following code connects with a web page and retrieves its html text.

Code: [Select]
' ########################################################################################
' The following example illustrates the use of the InternetOpen function.
' ########################################################################################

#COMPILE EXE
#DIM ALL
#INCLUDE "WININET.INC"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN () AS LONG

   LOCAL bResult AS LONG
   LOCAL hSession AS DWORD
   LOCAL hInternet AS DWORD
   LOCAL szUrl AS ASCIIZ * %INTERNET_MAX_PATH_LENGTH
   LOCAL strBuffer AS STRING
   LOCAL strData AS STRING
   LOCAL cbBytesRead AS DWORD

   ' Initializes an application's use of the WinINet functions.
   hSession = InternetOpen("Test", %INTERNET_OPEN_TYPE_DIRECT, "", "", 0)
   IF hSession = %NULL THEN
      ? "InternetOpen error:" & STR$(GetLastError)
      #IF %DEF(%PB_CC32)
         WAITKEY$
      #ENDIF
      EXIT FUNCTION
   END IF

   ' Opens a resource specified by a complete HTTP URL.
   szUrl = "http://www.powerbasic.com/"
   hInternet = InternetOpenUrl(hSession, szUrl, "", 0, %INTERNET_FLAG_NO_CACHE_WRITE, 0)
   IF hInternet = %NULL THEN
      ? "InternetOpenUrl error:" & STR$(GetLastError)
      InternetCloseHandle hSession
      #IF %DEF(%PB_CC32)
         WAITKEY$
      #ENDIF
      EXIT FUNCTION
   END IF

   ' Reads the data in 1024 bytes chunks
   strBuffer = SPACE$(1024)
   DO
      bResult = InternetReadFile(hInternet, STRPTR(strBuffer), LEN(strBuffer), cbBytesRead)
      IF bResult = 0 OR cbBytesRead = 0 THEN EXIT DO
      IF cbBytesRead = LEN(strBuffer) THEN
         strData = strData & strBuffer
      ELSE
         strData = strData & LEFT$(strBuffer, cbBytesRead)
      END IF
   LOOP

   ' Closes handles
   InternetCloseHandle hInternet
   InternetCloseHandle hSession

   ' Displays the retrieved data
   ? strData

   #IF %DEF(%PB_CC32)
      WAITKEY$
   #ENDIF

END FUNCTION
' ========================================================================================