I have a below code for WinHTTPRequest
:
DWORD WinHTTPRequest(LPCTSTR pServerName, LPCTSTR pRequest, WCHAR* sCommand, LPVOID pPostData, int nPostDataLength, LPCWSTR pwszHeaders, char **dataOut, int *nRead, WCHAR **dataHeaderOut, BOOL bTestProxy, BOOL bSecure, WCHAR* wsRedirect, DWORD *dwReturnStatus)
{
HINTERNET hCurrentOpen = NULL;
if (bTestProxy)
{
WCHAR sProxy[255] = L"";
GetProxy(sProxy);
if (lstrcmp(sProxy, L"") == 0)
hCurrentOpen = hOpen;
else if (lstrcmp(sProxy, g_wsCurrentProxy) != 0)
{
if (hOpenProxy)
WinHttpCloseHandle(hOpenProxy);
hOpenProxy = WinHttpOpen(L"Test", WINHTTP_ACCESS_TYPE_NAMED_PROXY, sProxy, NULL, 0/*INTERNET_FLAG_ASYNC*/);
lstrcpy(g_wsCurrentProxy, sProxy);
hCurrentOpen = hOpenProxy;
}
else
hCurrentOpen = hOpenProxy;
}
else
hCurrentOpen = hOpen;
HINTERNET hConnect = NULL;
if (bSecure)
hConnect = WinHttpConnect(hCurrentOpen, pServerName, INTERNET_DEFAULT_HTTPS_PORT, 0);
else
hConnect = WinHttpConnect(hCurrentOpen, pServerName, INTERNET_DEFAULT_HTTP_PORT, 0);
if (!hConnect)
{
DWORD dwError = GetLastError();
return dwError;
}
DWORD dwFlags;
if (bSecure)
dwFlags = WINHTTP_FLAG_SECURE | WINHTTP_FLAG_REFRESH;
else
dwFlags = WINHTTP_FLAG_REFRESH;
HINTERNET hRequest = WinHttpOpenRequest(hConnect, sCommand, pRequest, NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, dwFlags);
if (!hRequest)
{
DWORD dwError = GetLastError();
WinHttpCloseHandle(hConnect);
return dwError;
}
WinHttpAddRequestHeaders(hRequest, pwszHeaders, -1, WINHTTP_ADDREQ_FLAG_ADD);
int nLengthPostData;
if (nPostDataLength == NULL)
{
if (pPostData)
nLengthPostData = strlen((char*)pPostData);
else
nLengthPostData = 0;
}
else
nLengthPostData = nPostDataLength;
BOOL bSuccess;
if (wsRedirect != NULL)
{
DWORD dwOption;
DWORD dwOptionSize;
dwOption = WINHTTP_OPTION_REDIRECT_POLICY_NEVER;
dwOptionSize = sizeof(DWORD);
bSuccess = WinHttpSetOption(hRequest, WINHTTP_OPTION_REDIRECT_POLICY, (LPVOID)&dwOption, dwOptionSize);
DWORD dwOptionValue = WINHTTP_DISABLE_REDIRECTS;
bSuccess = WinHttpSetOption(hRequest, WINHTTP_OPTION_DISABLE_FEATURE, &dwOptionValue, sizeof(dwOptionValue));
}
BOOL b = WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, pPostData, pPostData == NULL ? 0 : nLengthPostData, nLengthPostData, 0);
if (!b)
{
DWORD dwError = GetLastError();
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hRequest);
return dwError;
}
WinHttpReceiveResponse(hRequest, NULL);
DWORD dwStatus = 0;
DWORD dwStatusSize = sizeof(DWORD);
if (WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER, NULL, &dwStatus, &dwStatusSize, NULL))
{
if (HTTP_STATUS_REDIRECT == dwStatus || HTTP_STATUS_MOVED == dwStatus)
{
DWORD dwSize;
WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_LOCATION, WINHTTP_HEADER_NAME_BY_INDEX, NULL, &dwSize, WINHTTP_NO_HEADER_INDEX);
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
return 500;
LPWSTR pwsRedirectURL = new WCHAR[dwSize];
bSuccess = WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_LOCATION, WINHTTP_HEADER_NAME_BY_INDEX, pwsRedirectURL, &dwSize, WINHTTP_NO_HEADER_INDEX);
if (!bSuccess)
return 500;
if (wsRedirect != NULL)
lstrcpy(wsRedirect, pwsRedirectURL);
if (dwReturnStatus != NULL)
*dwReturnStatus = dwStatus;
delete[] pwsRedirectURL;
}
else if (dwStatus != HTTP_STATUS_OK && dwStatus != HTTP_STATUS_BAD_REQUEST && dwStatus != HTTP_STATUS_CREATED)
{
DWORD dwError = GetLastError();
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hRequest);
if (dwReturnStatus != NULL)
*dwReturnStatus = dwStatus;
return dwError;
}
}
if (dataHeaderOut != NULL)
{
DWORD dwSize = 0;
WCHAR *pOutBuffer = NULL;
if (!WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_RAW_HEADERS_CRLF, WINHTTP_HEADER_NAME_BY_INDEX, NULL, &dwSize, WINHTTP_NO_HEADER_INDEX))
{
DWORD dwErr = GetLastError();
if (dwErr != ERROR_INSUFFICIENT_BUFFER)
{
DWORD dwError = GetLastError();
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hRequest);
return dwError;
}
}
pOutBuffer = new WCHAR[dwSize];
if (WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_RAW_HEADERS_CRLF, WINHTTP_HEADER_NAME_BY_INDEX, pOutBuffer, &dwSize, WINHTTP_NO_HEADER_INDEX))
{
pOutBuffer[dwSize] = '\0';
*dataHeaderOut = (WCHAR*)pOutBuffer;
}
//delete[] pOutBuffer;
}
char *sReadBuffer = NULL;
DWORD nTotalRead = 0;
DWORD nToRead = 0;
DWORD nBytesRead = 0;
do {
if (!WinHttpQueryDataAvailable(hRequest, &nToRead))
break;
if (nToRead == 0)
break;
sReadBuffer = (char*)((sReadBuffer == NULL) ? malloc(nToRead) : realloc(sReadBuffer, nTotalRead + nToRead + 1));
if (WinHttpReadData(hRequest, sReadBuffer + nTotalRead, nToRead, &nBytesRead))
{
nTotalRead += nBytesRead;
}
} while (nToRead > 0);
if (sReadBuffer != NULL && nTotalRead > 0)
{
{
char *sBuffer = new char[nTotalRead + 1];
memcpy(sBuffer, sReadBuffer, nTotalRead + 1);
sBuffer[nTotalRead] = '\0';
*dataOut = sBuffer;
}
free(sReadBuffer);
}
*nRead = nTotalRead;
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hRequest);
return ERROR_SUCCESS;
}
I am calling the above function as:
dwReturn = WinHTTPRequest(wsHostName, wsURLPathPost, L"POST", sPostData, NULL, wsAdditionalHeaders, &sHTTPData, &nDataRead, &wsDataHeader, 0, 0, wsRedirect, &dwStatus);
But at L"POST"
, its giving me below error:
Error (active) E0167 argument of type "const wchar_t *" is incompatible with parameter of type "WCHAR *"
Error C2664 'DWORD WinHTTPRequest(LPCTSTR,LPCTSTR,WCHAR *,LPVOID,int,LPCWSTR,char **,int *,WCHAR **,BOOL,BOOL,WCHAR *,DWORD *)': cannot convert argument 3 from 'const unsigned short [4]' to 'WCHAR *'
I tried changing the L"POST"
to _T("POST")
but didnt worked. How can I resolve this. Thanks
You take sCommand
as a pointer to non-const, suggesting that you plan to modify the buffer it points to. But you pass a string literal there, which cannot be modified. Either make the parameter const, or pass a modifiable buffer.