Author Topic: For those who need to do basic HTTP/HTTPS downloading (Windows Only)  (Read 2169 times)

0 Members and 1 Guest are viewing this topic.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Below is a function I made up that tries to take a dynamic approach to the very complex WinInet API functions. It is set up to dissect the URL you pass it so it can be sent to the functions correctly as well as call the functions with the right flags based on the security of the URL (HTTP or HTTPS). This just simply downloads the URL and stores it in the string. You could use this for downloading things other than text files or HTML files but depending on the size, you shouldn't. Here is the code:

Code: QB64: [Select]
  1. Option _Explicit 'not necessary, just to show that the code is Option _Explicit compatible
  2. $Console:Only 'Not necessary, just allows for scrolling
  3.  
  4. Print WebToString("http://www.schoolfreeware.com/Home.html")
  5.  
  6. Function WebToString$ (URL As String)
  7.     Const INTERNET_OPEN_TYPE_DIRECT = 1
  8.     Const INTERNET_DEFAULT_HTTP_PORT = 80
  9.     Const INTERNET_DEFAULT_HTTPS_PORT = 443
  10.     Const INTERNET_SERVICE_HTTP = 3
  11.     Const INTERNET_FLAG_SECURE = &H00800000
  12.     Const INTERNET_FLAG_RELOAD = &H80000000
  13.     Declare Dynamic Library "Wininet"
  14.         Function InternetOpen%& Alias InternetOpenA (ByVal lpszAgent As _Offset, Byval dwAccessType As Long, Byval lpszProxy As _Offset, Byval lpszProxyBypass As _Offset, Byval dwFlags As Long)
  15.         Function InternetConnect%& Alias InternetConnectA (ByVal hInternet As _Offset, Byval lpszServerName As _Offset, Byval nServerPort As Integer, Byval lpszUserName As _Offset, Byval lpszPassword As _Offset, Byval dwService As Long, Byval dwFlags As Long, Byval dwContext As _Offset)
  16.         Function HTTPOpenRequest%& Alias HttpOpenRequestA (ByVal hConnect As _Offset, Byval lpszVerb As _Offset, Byval lpszObjectName As _Offset, Byval lpszVersion As _Offset, Byval lpszReferrer As _Offset, Byval lpszAcceptTypes As _Offset, Byval dwFlags As Long, Byval dwContext As _Offset)
  17.         Function HTTPSendRequest%% Alias HttpSendRequestA (ByVal hRequest As _Offset, Byval lpszHeaders As _Offset, Byval dwHeadersLength As Long, Byval lpOptional As _Offset, Byval dwOptionalLength As Long)
  18.         Function InternetCloseHandle%% (ByVal hInternet As _Offset)
  19.         Function InternetReadFile%% (ByVal hFile As _Offset, Byval lpBuffer As _Offset, Byval dwNumberOfBytesToRead As Long, Byval lpdwNumberOfBytesRead As _Offset)
  20.     End Declare
  21.     Dim As _Offset hsession, httpsession, httpRequest
  22.     hsession = InternetOpen(0, INTERNET_OPEN_TYPE_DIRECT, 0, 0, 0)
  23.     Dim As String baseurl, location
  24.     If InStr(URL, "www") Then
  25.         baseurl = Mid$(URL, InStr(URL, "www") + Len("www."))
  26.     ElseIf InStr(URL, "//") Then
  27.         baseurl = Mid$(URL, InStr(URL, "//") + 2)
  28.     Else
  29.         baseurl = URL
  30.     End If
  31.     baseurl = Mid$(baseurl, 1, InStr(baseurl, "/") - 1)
  32.     location = Mid$(URL, InStr(URL, baseurl) + Len(baseurl))
  33.     baseurl = baseurl + Chr$(0)
  34.     location = location + Chr$(0)
  35.     If InStr(URL, "https") Then
  36.         httpsession = InternetConnect(hsession, _Offset(baseurl), INTERNET_DEFAULT_HTTPS_PORT, 0, 0, INTERNET_SERVICE_HTTP, 0, 0)
  37.     Else
  38.         httpsession = InternetConnect(hsession, _Offset(baseurl), INTERNET_DEFAULT_HTTP_PORT, 0, 0, INTERNET_SERVICE_HTTP, 0, 0)
  39.     End If
  40.     Dim As String sessiontype, accepttypes
  41.     sessiontype = "GET" + Chr$(0)
  42.     accepttypes = "*/*" + Chr$(0)
  43.     If InStr(URL, "https") Then
  44.         httpRequest = HTTPOpenRequest(httpsession, _Offset(sessiontype), _Offset(location), 0, 0, _Offset(accepttypes), INTERNET_FLAG_RELOAD Or INTERNET_FLAG_SECURE, 0)
  45.     Else
  46.         httpRequest = HTTPOpenRequest(httpsession, _Offset(sessiontype), _Offset(location), 0, 0, _Offset(accepttypes), INTERNET_FLAG_RELOAD, 0)
  47.     End If
  48.     Dim As _Byte sendrequest
  49.     sendrequest = HTTPSendRequest(httpRequest, 0, 0, 0, 0)
  50.     Dim As String szBuffer
  51.     szBuffer = Space$(4096)
  52.     Dim As Long dwRead
  53.     Dim As String webstring
  54.     While InternetReadFile(httpRequest, _Offset(szBuffer), Len(szBuffer), _Offset(dwRead)) And dwRead <> 0
  55.         webstring = webstring + Mid$(szBuffer, 1, dwRead)
  56.     Wend
  57.     Dim As _Byte closeh
  58.     closeh = InternetCloseHandle(hsession)
  59.     WebToString = webstring

Enjoy
« Last Edit: March 10, 2021, 03:15:57 pm by SpriggsySpriggs »
Shuwatch!

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: For those who need to do basic HTTP/HTTPS downloading (Windows Only)
« Reply #1 on: March 10, 2021, 01:01:24 pm »
I've edited the original post because I realize I forgot to include the variable for what types to accept. It probably isn't needed but it's for just-in-case
Shuwatch!

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: For those who need to do basic HTTP/HTTPS downloading (Windows Only)
« Reply #2 on: March 12, 2021, 02:57:42 am »
Is this preliminary TCP/IP setup?