Oded
2008-02-18 21:42:00 UTC
Hi,
I'm trying to use the wininet for uploading file to a server which requires
authentication. i checked a bit in the web and found that i have to first use
an empty request to perform the authentication and only afterward on the same
request send the data.
problem is - if i do that, the second request is sent empty (but it passes
the authentication), if i don't, the request is sent without the
authentication data (and failed).
any idea why?
here is the code:
hSession = InternetOpen("Test",
INTERNET_OPEN_TYPE_PRECONFIG,
NULL,
NULL,
0);
hConnection = InternetConnect( hSession,
server, // Server
INTERNET_INVALID_PORT_NUMBER,
NULL, // Username
NULL, // Password
INTERNET_SERVICE_HTTP,
0, // Synchronous
NULL); // No Context
INTERNET_BUFFERS BufferIn = {0};
DWORD dwBytesRead;
DWORD dwBytesWritten;
BYTE pBuffer[1024]; // Read from file in 1K chunks
BOOL bRead, bRet;
BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS );
HINTERNET hRequest = HttpOpenRequest(hConnection, "POST", targetURL, NULL,
NULL, NULL, INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE |
INTERNET_FLAG_KEEP_CONNECTION, 0);
if (!hRequest)
return OnExit(GetLastError());
// cchUserLength is the length of strUsername and
// cchPasswordLength is the length of strPassword.
char strUsername[64], strPassword[64];
strcpy (strUsername,"username");
strcpy (strPassword,"password");
DWORD cchUserLength = sizeof(strUsername);
DWORD cchPasswordLength = sizeof(strUsername);
InternetSetOption(hRequest, INTERNET_OPTION_USERNAME, strUsername,
cchUserLength+1);
InternetSetOption(hRequest, INTERNET_OPTION_PASSWORD, strPassword,
cchPasswordLength+1);
// at this point normal authentication logic can be used. If
// credentials are supplied in InternetConnect, then Wininet will
// resubmit credentials itself. See HttpDump Internet Client SDK sample
// for more information.
if (!HttpSendRequest (hRequest, NULL, 0, NULL, 0))
return OnExit(GetLastError());
// Read all returned data with InternetReadFile ()
do
{
InternetReadFile (hRequest, pBuffer, sizeof(pBuffer), &dwBytesRead);
}
while ( dwBytesRead != 0);
HANDLE hFile = CreateFile (uploadFile, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
return OnExit(GetLastError());
BufferIn.dwBufferTotal = GetFileSize (hFile, NULL);
if(!HttpSendRequestEx( hRequest, &BufferIn, NULL, HSR_INITIATE, 0))
return OnExit(GetLastError());
DWORD sum = 0;
do
{
if (!(bRead = ReadFile (hFile, pBuffer, sizeof(pBuffer), &dwBytesRead,
NULL)))
break;
if (!(bRet=InternetWriteFile( hRequest, pBuffer, dwBytesRead,
&dwBytesWritten)))
break;
sum += dwBytesWritten;
progress = sum;
}
while (dwBytesRead == sizeof(pBuffer)) ;
CloseHandle (hFile);
if(!HttpEndRequest(hRequest, NULL, 0, 0))
return OnExit(GetLastError());
return OnExit(0);
I'm trying to use the wininet for uploading file to a server which requires
authentication. i checked a bit in the web and found that i have to first use
an empty request to perform the authentication and only afterward on the same
request send the data.
problem is - if i do that, the second request is sent empty (but it passes
the authentication), if i don't, the request is sent without the
authentication data (and failed).
any idea why?
here is the code:
hSession = InternetOpen("Test",
INTERNET_OPEN_TYPE_PRECONFIG,
NULL,
NULL,
0);
hConnection = InternetConnect( hSession,
server, // Server
INTERNET_INVALID_PORT_NUMBER,
NULL, // Username
NULL, // Password
INTERNET_SERVICE_HTTP,
0, // Synchronous
NULL); // No Context
INTERNET_BUFFERS BufferIn = {0};
DWORD dwBytesRead;
DWORD dwBytesWritten;
BYTE pBuffer[1024]; // Read from file in 1K chunks
BOOL bRead, bRet;
BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS );
HINTERNET hRequest = HttpOpenRequest(hConnection, "POST", targetURL, NULL,
NULL, NULL, INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE |
INTERNET_FLAG_KEEP_CONNECTION, 0);
if (!hRequest)
return OnExit(GetLastError());
// cchUserLength is the length of strUsername and
// cchPasswordLength is the length of strPassword.
char strUsername[64], strPassword[64];
strcpy (strUsername,"username");
strcpy (strPassword,"password");
DWORD cchUserLength = sizeof(strUsername);
DWORD cchPasswordLength = sizeof(strUsername);
InternetSetOption(hRequest, INTERNET_OPTION_USERNAME, strUsername,
cchUserLength+1);
InternetSetOption(hRequest, INTERNET_OPTION_PASSWORD, strPassword,
cchPasswordLength+1);
// at this point normal authentication logic can be used. If
// credentials are supplied in InternetConnect, then Wininet will
// resubmit credentials itself. See HttpDump Internet Client SDK sample
// for more information.
if (!HttpSendRequest (hRequest, NULL, 0, NULL, 0))
return OnExit(GetLastError());
// Read all returned data with InternetReadFile ()
do
{
InternetReadFile (hRequest, pBuffer, sizeof(pBuffer), &dwBytesRead);
}
while ( dwBytesRead != 0);
HANDLE hFile = CreateFile (uploadFile, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
return OnExit(GetLastError());
BufferIn.dwBufferTotal = GetFileSize (hFile, NULL);
if(!HttpSendRequestEx( hRequest, &BufferIn, NULL, HSR_INITIATE, 0))
return OnExit(GetLastError());
DWORD sum = 0;
do
{
if (!(bRead = ReadFile (hFile, pBuffer, sizeof(pBuffer), &dwBytesRead,
NULL)))
break;
if (!(bRet=InternetWriteFile( hRequest, pBuffer, dwBytesRead,
&dwBytesWritten)))
break;
sum += dwBytesWritten;
progress = sum;
}
while (dwBytesRead == sizeof(pBuffer)) ;
CloseHandle (hFile);
if(!HttpEndRequest(hRequest, NULL, 0, 0))
return OnExit(GetLastError());
return OnExit(0);