I am trying to access file using WinHTTP from a web site and it fails for some web sites. I cannot use WinINet as WinINet does not support server implementations and cannot be used from a service.
It fails with error 12029 "The attempt to connect to the server failed", when WinHttpSendRequest() is executed for WEB_URL = "www.plusserver.com"
It works when WEB_URL = "www.google.com"
I can access both the web sites using Chrome web browser, so there is no connectivity issue. Maybe firewall on my end or the server end, am not sure if a firewall is blocking on a specific port.
Sample source code as below -
bool ReadFileFromInternet(LPCWSTR WEB_URL, DWORD dwSize, LPSTR pszOutBuffer)
{
DWORD dwDownloaded = 0;
BOOL bResults = FALSE;
HINTERNET hSession = NULL,
hConnect = NULL,
hRequest = NULL;
// Use WinHttpOpen to obtain a session handle.
hSession = WinHttpOpen(WEB_URL,
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS, 0);
// Specify an HTTP server.
if (hSession)
hConnect = WinHttpConnect(hSession, WEB_URL,
INTERNET_DEFAULT_HTTPS_PORT, 0);
int err = GetLastError();
// Create an HTTP request handle.
if (hConnect)
hRequest =
WinHttpOpenRequest(hConnect, L"GET", NULL,
NULL, WINHTTP_NO_REFERER,
WINHTTP_DEFAULT_ACCEPT_TYPES,
WINHTTP_FLAG_SECURE);
err = GetLastError();
// Send a request.
if (hRequest)
bResults = WinHttpSendRequest(hRequest,
WINHTTP_NO_ADDITIONAL_HEADERS, 0,
WINHTTP_NO_REQUEST_DATA, 0,
0, 0);
err = GetLastError();
// End the request.
if (bResults)
{
err = GetLastError();
bResults = WinHttpReceiveResponse(hRequest, NULL);
}
err = GetLastError();
if (bResults)
{
if (!WinHttpReadData(hRequest, (LPVOID)pszOutBuffer,
dwSize, &dwDownloaded))
printf("Error %u in WinHttpReadData.\n", GetLastError());
}
// Report any errors.
if (!bResults)
printf("Error %d has occurred.\n", GetLastError());
// Close any open handles.
if (hRequest) WinHttpCloseHandle(hRequest);
if (hConnect) WinHttpCloseHandle(hConnect);
if (hSession) WinHttpCloseHandle(hSession);
return bResults;
}
How to troubleshoot this further? Am using Windows-7 , 64-bit