May 15, 2024, 11:00:39 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


HTTP SSL example

Started by sapero, May 08, 2008, 12:00:33 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

sapero

May 08, 2008, 12:00:33 PM Last Edit: October 22, 2008, 01:23:55 PM by sapero
SSL
  (Secure Sockets Layer) A protocol that provides for encrypted communications on the Internet. SSL runs in a layer above TCP/IP and below HTTP, NNTP, and SMTP.

$include "windowssdk.inc"
$include "winhttp.inc"
$include "wchar.inc"

' SSL (Secure Sockets Layer) example
' compile for console


HINTERNET hOpen
HINTERNET hConnect
HINTERNET hRequest
IStream stream
HRESULT hr

while (1)

hOpen = WinHttpOpen(L"Aurora Console App", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,_
WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0)

if (hOpen = 0)
wprintf(L"WinHttpOpen failed (0x%.8X)\n", GetLastError())
goto break
endif

hConnect = WinHttpConnect(hOpen, L"www.codeproject.com", INTERNET_DEFAULT_HTTPS_PORT, 0)

if (hConnect = 0)
wprintf(L"WinHttpConnect failed (0x%.8X)\n", GetLastError())
goto break
endif

LPCWSTR types[2]
types[0] = L"text/html"
types[1] = 0

' use flag WINHTTP_FLAG_SECURE to initiate SSL
hRequest = WinHttpOpenRequest(hConnect, L"GET", L"KB/IP/NagTPortScanner.aspx",_
NULL, WINHTTP_NO_REFERER, &types, WINHTTP_FLAG_SECURE)

if (hRequest = 0)
wprintf(L"WinHttpOpenRequest failed (0x%.8X)\n", GetLastError())
goto break
endif

if (WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0) = 0)
wprintf(L"WinHttpSendRequest failed (0x%.8X)\n", GetLastError())
goto break
endif

if (WinHttpReceiveResponse(hRequest, 0) = 0)
wprintf(L"WinHttpReceiveResponse failed (0x%.8X)\n", GetLastError())
goto break
endif

' query remote file size, set haveContentLength on success and dwContentLength to the length
iwstring szContentLength[32]
DWORD cch
DWORD dwHeaderIndex
BOOL haveContentLength
cch = 64
dwHeaderIndex = WINHTTP_NO_HEADER_INDEX

haveContentLength = WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_CONTENT_LENGTH, NULL,_
&szContentLength, &cch, &dwHeaderIndex)

DWORD dwContentLength
if (haveContentLength) then dwContentLength = _wtoi(szContentLength)

' read the response into memory stream
hr = CreateStreamOnHGlobal(0, true, &stream)
if (hr)
wprintf(L"CreateStreamOnHGlobal failed (0x%.8X)\n", hr)
goto break
endif

' allocate buffer for streaming received data
pointer p
p = new(byte, 4096)
if (p = 0)
wprintf(L"failed to allocate buffer\n")
goto break
endif

' to receive all data, we need to enter a loop
DWORD dwReceivedTotal
dwReceivedTotal = 0
while (WinHttpQueryDataAvailable(hRequest, &cch) and cch)

if (cch > 4096) then cch = 4096
dwReceivedTotal += cch

' display number of received bytes
if (haveContentLength)

wprintf(L"received %d of %d (%d%%)%c", dwReceivedTotal, dwContentLength,_
dwReceivedTotal*100/dwContentLength, 13)
else
wprintf(L"received %d (unknown length)%c", dwReceivedTotal, 10)
endif

WinHttpReadData(hRequest, p, cch, &cch)

' write into stream
hr = stream->_Write(p, cch, NULL)
if (hr)
wprintf(L"failed to write data to stream (0x%.8X)\n", hr)
endif

endwhile

delete p
wprintf(L"\n\nreceived all data.\n")

' terminate the sream with a NULL
p = NULL
stream->_Write(&p, 1, NULL)

' get pointer to stream bytes
wprintf(L"getting HGLOBAL from stream...\n")
HGLOBAL hgl

hr = GetHGlobalFromStream(stream, &hgl)
if (hr)
wprintf(L"GetHGlobalFromStream failed (0x%.8X)\n", hr)
goto break
endif

wprintf(L"locking memory...\n")
p = GlobalLock(hgl)
if (p = 0)
wprintf(L"GlobalLock failed (0x%.8X)\n", GetLastError())
goto break
endif

wprintf(L"displaying received data...\n")
' terminate the string at 1024 bytes (MessageBox lag)
'if (dwReceivedTotal > 1024) then dwReceivedTotal = 1024
'*p[dwReceivedTotal] = 0

MessageBox(0, *<string>p, "", 0)
GlobalUnlock(hgl)

goto break
endwhile


break:
' delete stream and close handles
if (stream <> 0) then stream->Release()
if (hRequest) then WinHttpCloseHandle(hRequest)
if (hConnect) then WinHttpCloseHandle(hConnect)
if (hOpen) then WinHttpCloseHandle(hOpen)

_system("pause")