s***@gmail.com
2007-04-26 00:37:03 UTC
I'm new to winhttp programming.
Can anyone tell me whats wrong with this code??? I'm frustrated beyond
limits.
The WinHttpSendRequest fails with error 12007
(ERROR_WINHTTP_NAME_NOT_RESOLVED)
The url works fine when I use a browser.
int retVal = 0; //success
string url = "http://aaaa.bbbb.com:81/query?
tag=cccc&environment=dddd";
string machineList = "";
//Convert the url to Unicode because all WinHTTP API is UNICODE
int urlLength = (int)url.length();
LPWSTR _url = new wchar_t[urlLength + 1];
if (urlLength + 1 != MultiByteToWideChar(CP_ACP, 0, url.c_str(),
urlLength + 1, _url, urlLength + 1))
{
retVal = GetLastError();
if(_url)
{
delete[] _url;
_url = NULL;
}
return retVal; //error in converting to unicode
}
_url[urlLength] = 0;
// Extract hostname from URL
URL_COMPONENTS urlComp;
wchar_t hostName[MAX_PATH];
ZeroMemory(&urlComp, sizeof(urlComp));
urlComp.dwStructSize = sizeof(urlComp);
urlComp.lpszHostName = hostName;
urlComp.dwHostNameLength = MAX_PATH;
urlComp.dwUrlPathLength = 0xffffffff;
if (!WinHttpCrackUrl(_url, 0, 0, &urlComp))
{
retVal = GetLastError();
return retVal; //error while parsing url
}
hostName[urlComp.dwHostNameLength] = 0;
HINTERNET hSession = NULL,
hConnect = NULL,
hRequest = NULL;
do
{
hSession = WinHttpOpen(L"xxxx", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
NULL, NULL, 0);
if (NULL == hSession)
{
retVal = GetLastError();
break; //error while trying to initialize WinInet
}
hConnect = WinHttpConnect(hSession, hostName, urlComp.nPort, 0);
if (NULL == hConnect)
{
retVal = GetLastError();
break; //error while connecting to server
}
DWORD dwOpenRequestFlag= (INTERNET_SCHEME_HTTPS ==
urlComp.nScheme) ? WINHTTP_FLAG_SECURE : 0;
hRequest = WinHttpOpenRequest(hConnect,
L"GET",//NULL,
urlComp.lpszUrlPath,
NULL,
WINHTTP_NO_REFERER,
WINHTTP_DEFAULT_ACCEPT_TYPES,
dwOpenRequestFlag);//0);
if (NULL == hRequest)
{
retVal = GetLastError();
break; //error while creating request to server
}
if(!WinHttpSendRequest(hRequest,
WINHTTP_NO_ADDITIONAL_HEADERS,
0,
WINHTTP_NO_REQUEST_DATA,
0, 0, 0))
{
retVal = GetLastError();
break;
}
if (!WinHttpReceiveResponse(hRequest, NULL))
{
retVal = GetLastError();
break;
}
DWORD nrOfBytesAvailable;
if (!WinHttpQueryDataAvailable(hRequest, &nrOfBytesAvailable))
{
retVal = GetLastError();
break;
}
LPSTR data = new char[nrOfBytesAvailable + 1];
DWORD nrOfBytesRead;
if (!WinHttpReadData(hRequest, data, nrOfBytesAvailable,
&nrOfBytesRead))
{
retVal = GetLastError();
break;
}
if (nrOfBytesRead != nrOfBytesAvailable)
{
retVal = ERROR_BAD_ARGUMENTS;
break;
}
data[nrOfBytesAvailable] = '\0';
machineList = data;
}while(false);
if (hRequest)
{
WinHttpCloseHandle(hRequest);
}
if (hConnect)
{
WinHttpCloseHandle(hConnect);
}
if (hSession)
{
WinHttpCloseHandle(hSession);
}
if(_url)
{
delete[] _url;
_url = NULL;
}
return retVal;
Can anyone tell me whats wrong with this code??? I'm frustrated beyond
limits.
The WinHttpSendRequest fails with error 12007
(ERROR_WINHTTP_NAME_NOT_RESOLVED)
The url works fine when I use a browser.
int retVal = 0; //success
string url = "http://aaaa.bbbb.com:81/query?
tag=cccc&environment=dddd";
string machineList = "";
//Convert the url to Unicode because all WinHTTP API is UNICODE
int urlLength = (int)url.length();
LPWSTR _url = new wchar_t[urlLength + 1];
if (urlLength + 1 != MultiByteToWideChar(CP_ACP, 0, url.c_str(),
urlLength + 1, _url, urlLength + 1))
{
retVal = GetLastError();
if(_url)
{
delete[] _url;
_url = NULL;
}
return retVal; //error in converting to unicode
}
_url[urlLength] = 0;
// Extract hostname from URL
URL_COMPONENTS urlComp;
wchar_t hostName[MAX_PATH];
ZeroMemory(&urlComp, sizeof(urlComp));
urlComp.dwStructSize = sizeof(urlComp);
urlComp.lpszHostName = hostName;
urlComp.dwHostNameLength = MAX_PATH;
urlComp.dwUrlPathLength = 0xffffffff;
if (!WinHttpCrackUrl(_url, 0, 0, &urlComp))
{
retVal = GetLastError();
return retVal; //error while parsing url
}
hostName[urlComp.dwHostNameLength] = 0;
HINTERNET hSession = NULL,
hConnect = NULL,
hRequest = NULL;
do
{
hSession = WinHttpOpen(L"xxxx", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
NULL, NULL, 0);
if (NULL == hSession)
{
retVal = GetLastError();
break; //error while trying to initialize WinInet
}
hConnect = WinHttpConnect(hSession, hostName, urlComp.nPort, 0);
if (NULL == hConnect)
{
retVal = GetLastError();
break; //error while connecting to server
}
DWORD dwOpenRequestFlag= (INTERNET_SCHEME_HTTPS ==
urlComp.nScheme) ? WINHTTP_FLAG_SECURE : 0;
hRequest = WinHttpOpenRequest(hConnect,
L"GET",//NULL,
urlComp.lpszUrlPath,
NULL,
WINHTTP_NO_REFERER,
WINHTTP_DEFAULT_ACCEPT_TYPES,
dwOpenRequestFlag);//0);
if (NULL == hRequest)
{
retVal = GetLastError();
break; //error while creating request to server
}
if(!WinHttpSendRequest(hRequest,
WINHTTP_NO_ADDITIONAL_HEADERS,
0,
WINHTTP_NO_REQUEST_DATA,
0, 0, 0))
{
retVal = GetLastError();
break;
}
if (!WinHttpReceiveResponse(hRequest, NULL))
{
retVal = GetLastError();
break;
}
DWORD nrOfBytesAvailable;
if (!WinHttpQueryDataAvailable(hRequest, &nrOfBytesAvailable))
{
retVal = GetLastError();
break;
}
LPSTR data = new char[nrOfBytesAvailable + 1];
DWORD nrOfBytesRead;
if (!WinHttpReadData(hRequest, data, nrOfBytesAvailable,
&nrOfBytesRead))
{
retVal = GetLastError();
break;
}
if (nrOfBytesRead != nrOfBytesAvailable)
{
retVal = ERROR_BAD_ARGUMENTS;
break;
}
data[nrOfBytesAvailable] = '\0';
machineList = data;
}while(false);
if (hRequest)
{
WinHttpCloseHandle(hRequest);
}
if (hConnect)
{
WinHttpCloseHandle(hConnect);
}
if (hSession)
{
WinHttpCloseHandle(hSession);
}
if(_url)
{
delete[] _url;
_url = NULL;
}
return retVal;