Закачка из Интернета по протоколу https - возможно ли? - VB
Формулировка задачи:
У компонента MsInet есть свойство "протокол" и одно из значений HTTPS (при этом порт автоматически ставится верно - 443). Но данные не скачиваются.
Попытка вызвать API-функцию URLDownloadToFile тоже не приводят к результату. На StackOverflow нашел подходящий диалог: пользователь приводит пример попытки загрузить через https (не работает). Эксперт ему отвечает и приводит URL с http.
Решение задачи: «Закачка из Интернета по протоколу https - возможно ли?»
textual
Листинг программы
Option Explicit Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenW" (ByVal sAgent As Long, ByVal lAccessType As Long, ByVal sProxyName As Long, ByVal sProxyBypass As Long, ByVal lFlags As Long) As Long Private Declare Function InternetOpenUrl Lib "wininet.dll" Alias "InternetOpenUrlW" (ByVal hOpen As Long, ByVal sURL As Long, ByVal sHeaders As Long, ByVal lLength As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long Private Declare Function HttpQueryInfo Lib "wininet" Alias "HttpQueryInfoW" (ByVal hRequest As Long, ByVal dwInfoLevel As Long, lpBuffer As Any, ByRef lpdwBufferLength As Long, ByRef lpdwIndex As Long) As Long Private Declare Function InternetReadFile Lib "wininet.dll" (ByVal hFile As Long, sBuffer As Any, ByVal lNumBytesToRead As Long, lNumberOfBytesRead As Long) As Integer Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Integer Private Declare Function InternetSetFilePointer Lib "wininet" (ByRef hFile As Long, ByVal lDistanceToMove As Long, pReserved As Any, ByVal dwMoveMethod As Long, ByVal dwContext As Long) As Long Private Const FILE_BEGIN As Long = 0 Private Const INTERNET_OPEN_TYPE_PRECONFIG As Long = 0 Private Const INTERNET_FLAG_RELOAD As Long = &H80000000 Private Const HTTP_QUERY_CONTENT_LENGTH As Long = 5 Private Const HTTP_QUERY_FLAG_NUMBER As Long = &H20000000 Private Sub Form_Load() Dim hInternet As Long Dim hURL As Long Dim size As Long Dim arr() As Byte Dim ret As Long hInternet = InternetOpen(StrPtr(App.ProductName), INTERNET_OPEN_TYPE_PRECONFIG, 0, 0, 0) If hInternet = 0 Then Exit Sub hURL = InternetOpenUrl(hInternet, StrPtr("https://msdn.microsoft.com/ru-ru/library/windows/desktop/aa384238(v=vs.85).aspx"), 0, 0, INTERNET_FLAG_RELOAD, 0) If hURL = 0 Then Exit Sub ret = HttpQueryInfo(hURL, HTTP_QUERY_CONTENT_LENGTH Or HTTP_QUERY_FLAG_NUMBER, size, Len(size), 0) If ret = 0 Then Exit Sub ReDim arr(size - 1) InternetSetFilePointer hURL, 0, ByVal 0, FILE_BEGIN, 0 ret = InternetReadFile(hURL, arr(0), size, 0) If ret = 0 Then Exit Sub InternetCloseHandle hURL InternetCloseHandle hInternet Debug.Print StrConv(arr(), vbUnicode) End Sub
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д