I am having a inproc activeX control from which I am trying to POST a file to
a server using HTTPS connection w/ multi-part header through ISA 2004 proxy
server. I am using HttpSendRequestEx(), InternetWriteFile() and
HttpEndRequest() to upload the file. But HttpEndRequest() always fails on
Windows 2000 IE 6.0 SP1 w/ error code 12152.
This activeX control is called from a webpage which is also accessing HTTPS
pages on the server.
Also, on Windows XP SP2, I have to set the proxy user name and password
using InternetSetOptions() or else it fails too.
The file uploaded without any problems if done directly without proxy
Has anyone got into this problem?
Here is the the psudo code which does this :
***********************************************************
int PostData()
{
bool bSuccess = FALSE;
HINTERNET hInternet = InternetOpenA(
"Post Data",
INTERNET_OPEN_TYPE_PRECONFIG,
NULL,
NULL,
0
);
if (!hInternet )
{
return FALSE;
}
DWORD dwService = INTERNET_SERVICE_HTTP;
DWORD dwFlags = NULL;
DWORD dwContext = 0; //synchronous transfer
HINTERNET hConnection = InternetConnectA(hInternet,
m_lpszURL, (INTERNET_PORT)m_ulPort,
NULL,NULL,
dwService,dwFlags,
dwContext);
if ( !hConnection )
{
InternetCloseHandle(hInternet);
return TRUE;
}
CHAR* ppszAccept[2]={"*/*",0};
dwFlags = INTERNET_FLAG_RELOAD |
INTERNET_FLAG_NO_CACHE_WRITE |
INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP |
INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS |
INTERNET_FLAG_IGNORE_CERT_DATE_INVALID |
INTERNET_FLAG_IGNORE_CERT_CN_INVALID |
INTERNET_FLAG_KEEP_CONNECTION;
if (m_ulPort == INTERNET_DEFAULT_HTTPS_PORT)
dwFlags |= INTERNET_FLAG_SECURE;
HINTERNET hRequest = HttpOpenRequestA(hConnection,
"POST",
"\uploadurl",
NULL,
NULL,
(LPCSTR *)ppszAccept,
dwFlags,
NULL
);
if (!hRequest)
{
InternetCloseHandle(hConnection);
InternetCloseHandle(hInternet);
return TRUE;
}
DWORD dwSecuFlags = 0;
DWORD dwBufLen = sizeof (dwSecuFlags);
BOOL ok = InternetQueryOption (hRequest, INTERNET_OPTION_SECURITY_FLAGS,
&dwSecuFlags, &dwBufLen);
dwSecuFlags |= SECURITY_FLAG_IGNORE_UNKNOWN_CA |
SECURITY_FLAG_IGNORE_CERT_DATE_INVALID |
SECURITY_FLAG_IGNORE_CERT_CN_INVALID |
SECURITY_FLAG_IGNORE_WRONG_USAGE;
if (!InternetSetOption (hRequest, INTERNET_OPTION_SECURITY_FLAGS,
&dwSecuFlags, sizeof(dwSecuFlags)))
{
Log("CHICManager::UploadResults():: InternetSetOption(Error:%ld)
FAILED \r\n", GetLastError() );
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnection);
InternetCloseHandle(hInternet);
return TRUE;
}
CStringA strHeader;
CHAR szCookie[512];
memset(szCookie, 0, sizeof(szCookie));
sprintf(szCookie, "Cookie: %s=%s", "CYCYCY", m_lpszCookie);
CHAR szBoundary[256];
CHAR szFileHeader[512];
CHAR szFileFooter[512];
memset(szFileHeader, 0, sizeof(szFileHeader));
memset(szFileFooter, 0, sizeof(szFileFooter));
strcpy(szBoundary, "-------------12322323");
strHeader += szCookie;
strHeader += "\r\n";
strHeader += "Content-Type: multipart/form-data; boundary=";
strHeader += szBoundary;
strHeader += "\r\n";
strHeader += "Accept-Language: ";
strHeader += "en-us";
strHeader += "\r\n";
strHeader += "Results: Results=true"; //This is custom header we need
int nProxyAuthRetry = 0;
INTERNET_BUFFERSA BufferIn;
GetFileHeader(szFileHeader, szBoundary); //This generates some unique
header
GetFileFooter(szFileFooter, szBoundary); //This generates some unique
footer
bool bRetry = false;
DWORD dwBufferLength = 1024;
DWORD dwOption = INTERNET_OPTION_PROXY;
BYTE lpBuffer[1024];
//I have to do Proxy Auth. here or else it does not work even in case of
Windows XP https
if ( !m_bProxyAuthenticated && InternetQueryOption(hInternet, dwOption,
lpBuffer, &dwBufferLength) )
{
INTERNET_PROXY_INFO *pProxyInfo = (INTERNET_PROXY_INFO *)lpBuffer;
if ( pProxyInfo->dwAccessType == INTERNET_OPEN_TYPE_PROXY )
{
GetProxyAuthParams(); //This displays dialog to get user name
and password for proxy auth.
SetProxyAuthetication(hRequest); //this sets the user name and
password
}
}
again :
BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS );
BufferIn.Next = NULL;
BufferIn.lpcszHeader = strHeader;
BufferIn.dwHeadersLength = strHeader.GetLength();
BufferIn.dwHeadersTotal = strHeader.GetLength();
BufferIn.lpvBuffer = NULL;
BufferIn.dwBufferLength = 0;
BufferIn.dwBufferTotal = ( szErrorInfo ? strlen(szFileHeader) +
strlen(szFileFooter) : m_ulTotalDataSize + strlen(szFileHeader) +
strlen(szFileFooter));
BufferIn.dwOffsetLow = 0;
BufferIn.dwOffsetHigh = 0;
if(!HttpSendRequestExA( hRequest, &BufferIn, NULL, HSR_INITIATE |
HSR_SYNC, 0))
{
if ( IsProxyAuthRequired(hRequest) ) //this will check if the error
was because we still need to set the password
{
GetProxyAuthParams();
SetProxyAuthetication(hRequest);
goto again;
}
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnection);
InternetCloseHandle(hInternet);
return FALSE;
}
ULONG ulBytesToWrite;
ULONG ulTotalBytesWritten = 0;
ULONG ulBytesWritten = 0;
bSuccess = TRUE;
if ( szErrorInfo == NULL )
{
//First send the Boundary prefix required for the file
if (!InternetWriteFile( hRequest, szFileHeader,
(DWORD)strlen(szFileHeader), &ulBytesWritten) )
{
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnection);
InternetCloseHandle(hInternet);
return FALSE;
}
//Now send the actual file
ulBytesToWrite = m_ulTotalDataSize;
ULONG ulIndex = 0;
while (ulIndex < m_ulTotalDataSize )
{
if (!InternetWriteFile( hRequest, &(m_lpDataBuffer[ulIndex]),
ulBytesToWrite, &ulBytesWritten) )
{
bSuccess = FALSE;
break;
}
ulIndex += ulBytesWritten;
ulBytesToWrite = ( m_ulTotalDataSize - ulIndex );
}
if( !bSuccess )
{
Log( "CHICManager::UploadResults():: ERROR on InternetWriteFile
%ld \r\n", GetLastError() );
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnection);
InternetCloseHandle(hInternet);
return FALSE;
}
}
//Now send the end Boundary prefix required for the file
if ( !InternetWriteFile( hRequest, szFileFooter, strlen(szFileFooter),
&ulBytesWritten) )
{
//Log GetLastError()
}
if( !HttpEndRequestA(hRequest, NULL, 0, 0) )
{
//Log GetLastError()
bSuccess = FALSE;
}
else
{
bSuccess = TRUE;
}
//Read and discard any data sent by the server
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnection);
InternetCloseHandle(hInternet);
return bSuccess;
}
***********************************************************
1. While using HttpSendRequestEx/HttpEndRequest, HttpEndRequest fails w/
error ERROR_INTERNET_FORCE_RETRY and when I retry, it does not send anything
to the server.