Mo
2006-09-07 01:14:06 UTC
Hello All,
i am really new to the WinInet world, i am developing an application
where
my app will request a server listing, each user
will have to login, upon successful login the server will print out
the file listing for that particular user
the server is local public machine http://public.xx.yyyy.com/zzzz
if i add the username and password to the above URL the php script
will then print the user listing
however, i dont want to post the URL in my app, i would like to POST
the username/password pair using HTTP headers
so i used the following schema
InternetOpen()
InternetConnect()
HttpOpenRequest()
HttpAddRequestHeaders()
HttpSendRequest()
InternetReadFile()
however HttpSendRequest fails and it returns
ERROR_INTERNET_NAME_NOT_RESOLVED, InternetReadFile() also fails
if i do this from my pc browser it works fine
http://public.xx.yyyy.com/zzzz/list/?user=user1&pass=password1
is there is anything i am missing? my code is below
the error code is confusing since the above request works from my
browser
appreciate your help
Mo
<code>
//open an internet connection
hOpenHandle = InternetOpen(szAgent,INTERNET_OPEN_TYPE_DIRECT, NULL,
NULL, NULL);
if(NULL == hOpenHandle)
{
dwErrorCode = GetLastError();
printf("dwErrorCode=%x\r\n",dwErrorCode);
return FALSE;
}
hConnectHandle = InternetConnect(hOpenHandle
,TEXT("http://public.xx.yyyy.com/zzzz"),
INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP,
INTERNET_FLAG_PASSIVE, 0 );
if (NULL ==hConnectHandle)
{
dwErrorCode=GetLastError();
printf("dwErrorCode=%x\r\n",dwErrorCode);
return FALSE;
}
hHttpOpenRequest = HttpOpenRequest(hConnectHandle,
TEXT("POST"),TEXT("/list/?"),TEXT("HTTP/1.0"), NULL, 0,
INTERNET_FLAG_RELOAD, 0);
if (NULL == hHttpOpenRequest || GetLastError() ==
ERROR_INTERNET_NAME_NOT_RESOLVED)
{
InternetCloseHandle(hConnectHandle);
InternetCloseHandle(hOpenHandle);
printf("HttpOpenRequest failed, error = %d (0x%x)\n",GetLastError());
return 0;
}
TCHAR sHeader[] = TEXT("Content-Type:
application/x-www-form-urlencoded\r\n");
size_t length;
StringCchLength(sHeader,MAX_PATH,&length);
bResult = HttpAddRequestHeaders(hHttpOpenRequest,
sHeader, length, HTTP_ADDREQ_FLAG_REPLACE|HTTP_ADDREQ_FLAG_ADD);
if(!bResult)
{
dwErrorCode=GetLastError();
printf("Error code =%d",dwErrorCode);
}
TCHAR lpszPostData[] = TEXT("user=username1&password=password1\r\n");
StringCchLength(lpszPostData,MAX_PATH,&length);
bResult = HttpSendRequest(hHttpOpenRequest,
NULL,
0,
lpszPostData,
length);
if(!bResult)
{
dwErrorCode=GetLastError();
printf("Error code =%d",dwErrorCode);
}
//read the server listing
while(InternetReadFile( hHttpOpenRequest, buffer, sizeof(buffer),
&dwRead ))
{
if ( dwRead == 0 )
break;
buffer[dwRead] = 0;
dwTotalBytesRead+=dwRead;
}
</code>
i am really new to the WinInet world, i am developing an application
where
my app will request a server listing, each user
will have to login, upon successful login the server will print out
the file listing for that particular user
the server is local public machine http://public.xx.yyyy.com/zzzz
if i add the username and password to the above URL the php script
will then print the user listing
however, i dont want to post the URL in my app, i would like to POST
the username/password pair using HTTP headers
so i used the following schema
InternetOpen()
InternetConnect()
HttpOpenRequest()
HttpAddRequestHeaders()
HttpSendRequest()
InternetReadFile()
however HttpSendRequest fails and it returns
ERROR_INTERNET_NAME_NOT_RESOLVED, InternetReadFile() also fails
if i do this from my pc browser it works fine
http://public.xx.yyyy.com/zzzz/list/?user=user1&pass=password1
is there is anything i am missing? my code is below
the error code is confusing since the above request works from my
browser
appreciate your help
Mo
<code>
//open an internet connection
hOpenHandle = InternetOpen(szAgent,INTERNET_OPEN_TYPE_DIRECT, NULL,
NULL, NULL);
if(NULL == hOpenHandle)
{
dwErrorCode = GetLastError();
printf("dwErrorCode=%x\r\n",dwErrorCode);
return FALSE;
}
hConnectHandle = InternetConnect(hOpenHandle
,TEXT("http://public.xx.yyyy.com/zzzz"),
INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP,
INTERNET_FLAG_PASSIVE, 0 );
if (NULL ==hConnectHandle)
{
dwErrorCode=GetLastError();
printf("dwErrorCode=%x\r\n",dwErrorCode);
return FALSE;
}
hHttpOpenRequest = HttpOpenRequest(hConnectHandle,
TEXT("POST"),TEXT("/list/?"),TEXT("HTTP/1.0"), NULL, 0,
INTERNET_FLAG_RELOAD, 0);
if (NULL == hHttpOpenRequest || GetLastError() ==
ERROR_INTERNET_NAME_NOT_RESOLVED)
{
InternetCloseHandle(hConnectHandle);
InternetCloseHandle(hOpenHandle);
printf("HttpOpenRequest failed, error = %d (0x%x)\n",GetLastError());
return 0;
}
TCHAR sHeader[] = TEXT("Content-Type:
application/x-www-form-urlencoded\r\n");
size_t length;
StringCchLength(sHeader,MAX_PATH,&length);
bResult = HttpAddRequestHeaders(hHttpOpenRequest,
sHeader, length, HTTP_ADDREQ_FLAG_REPLACE|HTTP_ADDREQ_FLAG_ADD);
if(!bResult)
{
dwErrorCode=GetLastError();
printf("Error code =%d",dwErrorCode);
}
TCHAR lpszPostData[] = TEXT("user=username1&password=password1\r\n");
StringCchLength(lpszPostData,MAX_PATH,&length);
bResult = HttpSendRequest(hHttpOpenRequest,
NULL,
0,
lpszPostData,
length);
if(!bResult)
{
dwErrorCode=GetLastError();
printf("Error code =%d",dwErrorCode);
}
//read the server listing
while(InternetReadFile( hHttpOpenRequest, buffer, sizeof(buffer),
&dwRead ))
{
if ( dwRead == 0 )
break;
buffer[dwRead] = 0;
dwTotalBytesRead+=dwRead;
}
</code>