Discussion:
Make SSL Request Using WinInet (uploading a file)
(too old to reply)
rranly
2004-09-14 14:15:28 UTC
Permalink
Having problems uploading a file to a secure site using WinInet. I found
this article,(http://support.microsoft.com/?kbid=168151) that is pretty
straight forward however, I am getting a Error on HttpSendRequestEx #997.

If I take the certificate off the IIS machine, I am able to change the
port in the InternetConnect call, and everything works well, as well as
remove "INTERNET_FLAG_SECURE" flag in the HttpOpenRequest call, and I am
able to successfully upload the file.

INTERNET_BUFFERS BufferIn = {0};
DWORD dwBytesRead;
DWORD dwBytesWritten;
BYTE pBuffer[1024]; // Read from file in 1K chunks
bool bRead, bReturn = FALSE; //assume error
HINSTANCE wininetDLL;
long retCode = 0;
bool bErrorReturn = TRUE;

BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS );

wininetDLL = LoadLibrary("wininet.dll");
if(wininetDLL == NULL)
{
bReturn = FALSE;
return bReturn;
}
else
{
//Open Internet
HINTERNET hInternet = InternetOpen("Microsoft Internet Explorer",
INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
if(hInternet == NULL)
return FALSE;

//Open Connection
HINTERNET hConnect = InternetConnect(hInternet, Url, 443, "","",
INTERNET_SERVICE_HTTP, 0, 0);
if (hConnect == NULL)
return FALSE;

//Open Http Request
HINTERNET hRequest = HttpOpenRequest(hConnect, "PUT", FileName,
HTTP_VERSION, "", NULL, INTERNET_FLAG_SECURE, 0);
if (hRequest == NULL)
return FALSE;

//Create File
HANDLE hFile = CreateFile (FilePath, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
return FALSE;

BufferIn.dwBufferTotal = GetFileSize (hFile, NULL);
oLogFile << "File size is " << BufferIn.dwBufferTotal << "\n";

if(!HttpSendRequestEx( hRequest, &BufferIn, NULL, HSR_INITIATE, 0))
return FALSE;

DWORD sum = 0;
do
{
if (!(bRead = ReadFile (hFile, pBuffer, sizeof(pBuffer),
&dwBytesRead, NULL)))
{
return FALSE;
break;
}
if (!(bReturn=InternetWriteFile( hRequest, pBuffer, dwBytesRead,
&dwBytesWritten)))
{
return FALSE;
break;
}
sum += dwBytesWritten;
}
while (dwBytesRead == sizeof(pBuffer));

CloseHandle (hFile);
oLogFile << "Actual written bytes: " << sum << "\n";

if(!HttpEndRequest(hRequest, NULL, 0, 0))
return FALSE;

bReturn = InternetCloseHandle(hInternet);
bReturn = InternetCloseHandle(hConnect);
bReturn = InternetCloseHandle(hRequest);

bErrorReturn = RecordError(CrystalInterfaceHeaderId, FileName,
"Success: File sent.");

oLogFile.clear();
oLogFile.close();
bReturn = TRUE;
}
}
return bReturn;
Dhananjaya.R
2004-09-15 13:10:16 UTC
Permalink
Error #997 is a windows error ... it Meant I/O pending so its better to read
the entire file into some sort of buffer before posting it to the wininet.

//

// MessageId: ERROR_IO_PENDING

//

// MessageText:

//

// Overlapped I/O operation is in progress.

//

#define ERROR_IO_PENDING 997L // dderror


--
Regards
Dhananjaya R
Post by rranly
Having problems uploading a file to a secure site using WinInet. I found
this article,(http://support.microsoft.com/?kbid=168151) that is pretty
straight forward however, I am getting a Error on HttpSendRequestEx #997.
If I take the certificate off the IIS machine, I am able to change the
port in the InternetConnect call, and everything works well, as well as
remove "INTERNET_FLAG_SECURE" flag in the HttpOpenRequest call, and I am
able to successfully upload the file.
INTERNET_BUFFERS BufferIn = {0};
DWORD dwBytesRead;
DWORD dwBytesWritten;
BYTE pBuffer[1024]; // Read from file in 1K chunks
bool bRead, bReturn = FALSE; //assume error
HINSTANCE wininetDLL;
long retCode = 0;
bool bErrorReturn = TRUE;
BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS );
wininetDLL = LoadLibrary("wininet.dll");
if(wininetDLL == NULL)
{
bReturn = FALSE;
return bReturn;
}
else
{
//Open Internet
HINTERNET hInternet = InternetOpen("Microsoft Internet Explorer",
INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
if(hInternet == NULL)
return FALSE;
//Open Connection
HINTERNET hConnect = InternetConnect(hInternet, Url, 443, "","",
INTERNET_SERVICE_HTTP, 0, 0);
if (hConnect == NULL)
return FALSE;
//Open Http Request
HINTERNET hRequest = HttpOpenRequest(hConnect, "PUT", FileName,
HTTP_VERSION, "", NULL, INTERNET_FLAG_SECURE, 0);
if (hRequest == NULL)
return FALSE;
//Create File
HANDLE hFile = CreateFile (FilePath, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
return FALSE;
BufferIn.dwBufferTotal = GetFileSize (hFile, NULL);
oLogFile << "File size is " << BufferIn.dwBufferTotal << "\n";
if(!HttpSendRequestEx( hRequest, &BufferIn, NULL, HSR_INITIATE, 0))
return FALSE;
DWORD sum = 0;
do
{
if (!(bRead = ReadFile (hFile, pBuffer, sizeof(pBuffer),
&dwBytesRead, NULL)))
{
return FALSE;
break;
}
if (!(bReturn=InternetWriteFile( hRequest, pBuffer, dwBytesRead,
&dwBytesWritten)))
{
return FALSE;
break;
}
sum += dwBytesWritten;
}
while (dwBytesRead == sizeof(pBuffer));
CloseHandle (hFile);
oLogFile << "Actual written bytes: " << sum << "\n";
if(!HttpEndRequest(hRequest, NULL, 0, 0))
return FALSE;
bReturn = InternetCloseHandle(hInternet);
bReturn = InternetCloseHandle(hConnect);
bReturn = InternetCloseHandle(hRequest);
bErrorReturn = RecordError(CrystalInterfaceHeaderId, FileName,
"Success: File sent.");
oLogFile.clear();
oLogFile.close();
bReturn = TRUE;
}
}
return bReturn;
Loading...