I am trying to send and retrieve data to/from a remote client computer. I can't use DCOM or Named Pipes because ports 135 and 445 are closed and cannot be opened.
The advantage of winhttp, according to Microsoft docs, is you can use http as the transport (instead of named pipes) without having an IIS web server on the other end.
I'd consider using TCP, but I can't find any examples of using TCP in C/C++, and at this point I'm not sure if any exist?
I think I've got the basic connection down of the request, but how do I send information to the other computer? And how does the other server send it back?
Here's my code for doing a GET for Winhttp:
#include <windows.h>
#include <tchar.h>
#include <winhttp.h>
#include <strsafe.h>
#define tnew(nCharacters) new CHAR[nCharacters]()
#pragma comment(lib, "winhttp.lib")
int main()
{
CHAR *sResult = tnew(1024);
DWORD dwSize = 0, dwDownloaded = 0;
LPSTR pszOutBuffer = NULL;
BOOL bResults = FALSE;
HINTERNET hSession = NULL, hConnect = NULL, hRequest = NULL;
// Use WinHttpOpen to obtain a session handle.
hSession = WinHttpOpen(_T("WinHTTP Example/1.0"), WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
if (hSession)
hConnect = WinHttpConnect(hSession, L"www.microsoft.com", INTERNET_DEFAULT_HTTPS_PORT, 0); // Specify an HTTP server.
if (hConnect)
hRequest = WinHttpOpenRequest(hConnect, _T("GET"), NULL, NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_SECURE); // Create an HTTP request handle.
if (hRequest)
bResults = WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0); // Send a request.
if (bResults)
bResults = WinHttpReceiveResponse(hRequest, NULL); // End the request.
if (bResults) // Keep checking for data until there is nothing left.
{
do
{
dwSize = 0;
if (WinHttpQueryDataAvailable(hRequest, &dwSize)) // Check for available data.
{
pszOutBuffer = new CHAR[dwSize + 1];
ZeroMemory(pszOutBuffer, dwSize + 1);
if (WinHttpReadData(hRequest, (LPVOID)pszOutBuffer, dwSize, &dwDownloaded)) // Read the data.
StringCchPrintfA(sResult, 1024, "%s%s", sResult, pszOutBuffer);
}
}
while (dwSize > 0);
}
delete[] pszOutBuffer;
if (hRequest) WinHttpCloseHandle(hRequest);
if (hConnect) WinHttpCloseHandle(hConnect);
if (hSession) WinHttpCloseHandle(hSession);
}