I was trying to communicate using winHTTP for communication with the device using an IP address connected via ethernet cable. The connection and session are happening. However, winhttpreceiveresponse is always null. Here is my piece code which I have taken from here and adapted for my device:
#include "pch.h"
#include <windows.h>
#include <wininet.h>
#include <winInet.h>
#include <winhttp.h>
#pragma comment(lib, "winhttp.lib")
#include <stdio.h>
#include <iostream>
#include "http_dll.h"
DWORD dwError = ERROR_SUCCESS;
DWORD dwSize = 0;
DWORD dwDownloaded = 0;
LPSTR pszOutBuffer;
BOOL bResults = FALSE;
HINTERNET hSession = NULL,
hConnect = NULL,
hRequest = NULL;
int main(void) {
//Use WinHttpOpen to obtain a session handle.
hSession = WinHttpOpen(L"HTTP Connection",
WINHTTP_ACCESS_TYPE_AUTOMATIC_PROXY,
WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS, 0);
//Specify an HTTP server. // http://192.168.0.10/info
if (hSession)
hConnect = WinHttpConnect(hSession, L"192.168.0.10",
INTERNET_DEFAULT_PORT, 0);
printf("hSession %08x\n", hSession);
if (hSession == NULL)
{
dwError = GetLastError();
goto quit;
}
//Create an HTTP request handle.
if (hConnect)
hRequest = WinHttpOpenRequest(hConnect, L"GET", L"/version",
NULL, WINHTTP_NO_REFERER,
WINHTTP_DEFAULT_ACCEPT_TYPES,
WINHTTP_FLAG_SECURE);
//Send a request.
if (hRequest)
bResults = WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0,
WINHTTP_NO_REQUEST_DATA, 0, 0, 0);
printf("bResults %08x\n", bResults);
//End the request.
if (bResults)
bResults = WinHttpReceiveResponse(hRequest, NULL);
printf("bResults %08x\n", bResults);
//Keep checking for data until there is nothing left.
if (bResults)
{
do
{
//Check for available data.
dwSize = 0;
if (!WinHttpQueryDataAvailable(hRequest, &dwSize))
printf("Error %u in WinHttpQueryDataAvailable.\n",
GetLastError());
//Allocate space for the buffer.
pszOutBuffer = new char[dwSize + 1];
if (!pszOutBuffer)
{
printf("Out of memory\n");
dwSize = 0;
}
else
{
//Read the data.
ZeroMemory(pszOutBuffer, dwSize + 1);
if (!WinHttpReadData(hRequest, (LPVOID)pszOutBuffer,
dwSize, &dwDownloaded))
printf("Error %u in WinHttpReadData.\n", GetLastError());
else
printf("%s", pszOutBuffer);
//Free the memory allocated to the buffer.
delete[] pszOutBuffer;
}
} while (dwSize > 0);
}
//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);
quit:
if (dwError != ERROR_SUCCESS)
{
wprintf(L"Application failed with error: %u\n", dwError);
return -1;
}
return 0;
}
If someone could help if I am making any mistakes.Thanks
I have fixed the issue by changing the flag WINHTTP_FLAG_SECURE to WINHTTP_FLAG_ESCAPE_PERCENT in WinHttpReceiveResponse.