Получение страницы через прокси с авторизацией.
Мне нужно подключится к своему пхп скрипту, считать его вывод и записать внутри класса.
Для этого написал простенький метод класса, который это делает.
Проблема в том, что данный код не работает если установлен прокси с авторизацией. Как его правильно включить в настройки подключения?
Код:
BOOL CMyHTTP::sendRequest(void)
{
HINTERNET hSession = NULL;
HINTERNET hConnection = NULL;
HINTERNET hUrl = NULL;
BOOL hResult = FALSE;
try
{
LPCSTR proxyServerValue = (hasProxy()) ? proxyServer.c_str() : NULL;
INTERNET_PORT proxyPortValue = (hasProxy()) ? proxyPort : INTERNET_INVALID_PORT_NUMBER;
hSession = InternetOpen("Microsoft Internet Explorer", INTERNET_OPEN_TYPE_DIRECT, proxyServerValue, NULL, 0); //INTERNET_OPEN_TYPE_PRECONFIG
if (hSession == NULL)
throw ECantConnect();
hConnection = InternetConnect(hSession, urlServer.c_str(), proxyPortValue, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
if (hConnection == NULL)
throw ECantConnect();
LPCTSTR lpszVerb = "POST";
std::string urlAddr = urlSuffix + urlScript;
LPCTSTR lpszObjectName = urlAddr.c_str();
LPCTSTR lpszVersion = NULL; // Use default.
LPCTSTR lpszReferrer = NULL; // No referrer.
LPCTSTR *lplpszAcceptTypes = NULL; // Whatever the server wants to give us.
DWORD dwOpenRequestFlags = INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP |
INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS |
INTERNET_FLAG_KEEP_CONNECTION |
INTERNET_FLAG_NO_AUTH |
INTERNET_FLAG_NO_AUTO_REDIRECT |
INTERNET_FLAG_NO_COOKIES |
INTERNET_FLAG_NO_UI |
INTERNET_FLAG_RELOAD;
DWORD dwOpenRequestContext = 0;
hUrl = HttpOpenRequest(hConnection, lpszVerb, lpszObjectName, lpszVersion, lpszReferrer, lplpszAcceptTypes, dwOpenRequestFlags, dwOpenRequestContext);
if (hUrl == NULL)
throw ECantConnect();
std::string boundary = generateMultipartBoundary();
std::string contentTypeHeader = "Content-Type: multipart/form-data; boundary=" + boundary;
HttpAddRequestHeaders(hUrl, contentTypeHeader.c_str(), -1, HTTP_ADDREQ_FLAG_ADD);
std::string requestData = formPOSTsRequest(boundary);
LPVOID lpOptional = (LPVOID)(requestData.data());
DWORD dwOptionalLength = (DWORD)requestData.size();
DWORD dwStatus;
DWORD dwStatusSize = sizeof(dwStatus);
resend:
if (!HttpSendRequest(hUrl, NULL, 0, lpOptional, dwOptionalLength))
throw ECantGetData();
if (!HttpQueryInfo(hUrl, HTTP_QUERY_FLAG_NUMBER | HTTP_QUERY_STATUS_CODE, &dwStatus, &dwStatusSize, NULL))
throw ECantGetData();
switch (dwStatus)
{
case HTTP_STATUS_PROXY_AUTH_REQ: // Proxy Authentication Required
if (!hasAuth())
throw ENeedProxyAuth();
InternetSetOption(hUrl, INTERNET_OPTION_PROXY_USERNAME,
(LPVOID)(authUsername.data()), (DWORD)(authUsername.length()+1));
InternetSetOption(hUrl, INTERNET_OPTION_PROXY_PASSWORD,
(LPVOID)(authPassword.data()), (DWORD)(authPassword.length()+1));
goto resend;
break;
case HTTP_STATUS_DENIED: // Server Authentication Required
throw ENeedServerAuth();
break;
}
if (reqResult != NULL)
{
delete [] reqResult->data;
delete reqResult;
reqResult = NULL;
}
char* cBuffer = new char[MAX_BUFFER_LENGTH];
std::string output;
DWORD dwBytesRead = 0;
InternetReadFile(hUrl, (LPSTR)cBuffer, (DWORD)MAX_BUFFER_LENGTH, &dwBytesRead);
while (dwBytesRead > 0)
{
output.append(cBuffer, dwBytesRead);
InternetReadFile(hUrl, (LPSTR)cBuffer, (DWORD)MAX_BUFFER_LENGTH, &dwBytesRead);
}
delete [] cBuffer;
reqResult = new requestResult;
reqResult->dataSize = 0;
reqResult->data = NULL;
size_t pos;
pos = output.find(RETURNFLAG_OK);
if (pos != -1)
reqResult->status = psrOK;
else
{
pos = output.find(RETURNFLAG_ERROR);
if (pos != -1)
reqResult->status = psrError;
else
throw ECantGetData();
}
pos = output.find('\n');
if (pos != -1)
output.erase(0, pos + 1);
else
output.clear();
reqResult->data = new char[output.length()];
std::basic_string <char>::pointer data = reqResult->data;
size_t len = output.copy(data, output.length());
if (len == output.length())
{
reqResult->dataSize = (int)len;
hResult = TRUE;
}
else
lastError = E_NOTENOUGHMEM;
}
catch(ECantConnect) {lastError = E_CANTCONNECT;}
catch(ECantGetData) {lastError = E_CANTGETDATA;}
catch(ENeedProxyAuth) {lastError = E_PROXYAUTH;}
catch(ENeedServerAuth) {lastError = E_SERVERAUTH;}
if (hUrl != NULL) InternetCloseHandle(hUrl);
if (hConnection != NULL) InternetCloseHandle(hConnection);
if (hSession != NULL) InternetCloseHandle(hSession);
return hResult;
}
Может кто знает готовый класс? Искал я в интернете, но ничего не нашёл.
Спасибо.