Discussion:
winhttpsendrequest error 12007 (ERROR_WINHTTP_NAME_NOT_RESOLVED)
(too old to reply)
s***@gmail.com
2007-04-26 00:37:03 UTC
Permalink
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;
Marcin Domaslawski
2007-04-26 07:34:14 UTC
Permalink
Hi,

I've checked to WinHttpSendRequest line and works fine. Have you checked on
an existing sever ? ;-)
ERROR_WINHTTP_NAME_NOT_RESOLVED = Server name cannot be resolved ;-)

Marcin Domaslawski
Post by s***@gmail.com
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;
deepakk
2007-05-16 21:12:21 UTC
Permalink
Post by Marcin Domaslawski
Hi,
I've checked to WinHttpSendRequest line and works fine. Have you checked on
an existing sever ? ;-)
ERROR_WINHTTP_NAME_NOT_RESOLVED = Server name cannot be resolved ;-)
Marcin Domaslawski
Post by s***@gmail.com
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;- Hide quoted text -
- Show quoted text -
Try generating a trace to debug your issue
For Vista

http://blogs.msdn.com/wndp/archive/2007/03/21/winhttp-configuration-for-windows-vista.aspx

For downlevel OS
http://msdn2.microsoft.com/en-us/library/aa384119.aspx

Loading...