Gets a internet page through HTTP proxy
-
Here sample of a code to gets a page through proxy with authorizing. First
::HttpSendRequest
call returns success, but second call after proxy authorization is wedging. Why!? Where is mistake!?HINTERNET hOpenHandle, hConnectHandle, hResourceHandle;
DWORD dwError, dwStatus;
DWORD dwStatusSize = sizeof(dwStatus);
hOpenHandle = ::InternetOpen("Example", INTERNET_OPEN_TYPE_PROXY, strProxyName, NULL, 0);
if (NULL != hOpenHandle)
{
hConnectHandle = ::InternetConnect(hOpenHandle, "www.microsoft.com", INTERNET_INVALID_PORT_NUMBER, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
if (NULL != hConnectHandle)
{
hResourceHandle = ::HttpOpenRequest(hConnectHandle, "GET", "", HTTP_VERSION, NULL, 0, INTERNET_FLAG_DONT_CACHE, 0);
if (NULL != hResourceHandle)
{
resend:
BOOL bSendRequest = ::HttpSendRequest(hResourceHandle, NULL, 0, NULL, 0);
::HttpQueryInfo(hResourceHandle, HTTP_QUERY_FLAG_NUMBER | HTTP_QUERY_STATUS_CODE, &dwStatus, &dwStatusSize, NULL);
switch (dwStatus)
{
case HTTP_STATUS_PROXY_AUTH_REQ:
//Proxy Authentication Required
// Insert code to set strUsername and strPassword.
InternetSetOption(hResourceHandle, INTERNET_OPTION_PROXY_USERNAME, (LPVOID)(LPCTSTR)strProxyUsername, strlen(strProxyUsername)+1);
InternetSetOption(hResourceHandle, INTERNET_OPTION_PROXY_PASSWORD, (LPVOID)(LPCTSTR)strProxyPassword, strlen(strProxyPassword)+1);
goto resend;
break;
case HTTP_STATUS_DENIED:
//Server Authentication Required
// Insert code to set strUsername and strPassword.
InternetSetOption(hResourceHandle, INTERNET_OPTION_PROXY_USERNAME, (LPVOID)(LPCTSTR)strProxyUsername, strlen(strProxyUsername)+1);
InternetSetOption(hResourceHandle, INTERNET_OPTION_PROXY_PASSWORD, (LPVOID)(LPCTSTR)strProxyPassword, strlen(strProxyPassword)+1);
goto resend;
break;
}
// Insert code to read the information from the hResourceHandle
// at this point.
if (HTTP_STATUS_OK == dwStatus)
{
//Gets a page
}
::InternetCloseHandle(hResourceHandle);
}
::InternetCloseHandle(hConnectHandle);
}
::InternetCloseHandle(hOpenHandle);
}Best regards, Eugene Pustovoyt
-
Here sample of a code to gets a page through proxy with authorizing. First
::HttpSendRequest
call returns success, but second call after proxy authorization is wedging. Why!? Where is mistake!?HINTERNET hOpenHandle, hConnectHandle, hResourceHandle;
DWORD dwError, dwStatus;
DWORD dwStatusSize = sizeof(dwStatus);
hOpenHandle = ::InternetOpen("Example", INTERNET_OPEN_TYPE_PROXY, strProxyName, NULL, 0);
if (NULL != hOpenHandle)
{
hConnectHandle = ::InternetConnect(hOpenHandle, "www.microsoft.com", INTERNET_INVALID_PORT_NUMBER, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
if (NULL != hConnectHandle)
{
hResourceHandle = ::HttpOpenRequest(hConnectHandle, "GET", "", HTTP_VERSION, NULL, 0, INTERNET_FLAG_DONT_CACHE, 0);
if (NULL != hResourceHandle)
{
resend:
BOOL bSendRequest = ::HttpSendRequest(hResourceHandle, NULL, 0, NULL, 0);
::HttpQueryInfo(hResourceHandle, HTTP_QUERY_FLAG_NUMBER | HTTP_QUERY_STATUS_CODE, &dwStatus, &dwStatusSize, NULL);
switch (dwStatus)
{
case HTTP_STATUS_PROXY_AUTH_REQ:
//Proxy Authentication Required
// Insert code to set strUsername and strPassword.
InternetSetOption(hResourceHandle, INTERNET_OPTION_PROXY_USERNAME, (LPVOID)(LPCTSTR)strProxyUsername, strlen(strProxyUsername)+1);
InternetSetOption(hResourceHandle, INTERNET_OPTION_PROXY_PASSWORD, (LPVOID)(LPCTSTR)strProxyPassword, strlen(strProxyPassword)+1);
goto resend;
break;
case HTTP_STATUS_DENIED:
//Server Authentication Required
// Insert code to set strUsername and strPassword.
InternetSetOption(hResourceHandle, INTERNET_OPTION_PROXY_USERNAME, (LPVOID)(LPCTSTR)strProxyUsername, strlen(strProxyUsername)+1);
InternetSetOption(hResourceHandle, INTERNET_OPTION_PROXY_PASSWORD, (LPVOID)(LPCTSTR)strProxyPassword, strlen(strProxyPassword)+1);
goto resend;
break;
}
// Insert code to read the information from the hResourceHandle
// at this point.
if (HTTP_STATUS_OK == dwStatus)
{
//Gets a page
}
::InternetCloseHandle(hResourceHandle);
}
::InternetCloseHandle(hConnectHandle);
}
::InternetCloseHandle(hOpenHandle);
}Best regards, Eugene Pustovoyt
take a look at this example (listed at the bottom of the page) that is shown in MSDN: http://msdn.microsoft.com/en-us/library/aa384220%28VS.85%29.aspx[^]
Interface basics click here : http://www.codeproject.com/com/COMBasics.asp don't forget to vote :)