Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VC8.0
I want to upload a local file(ReadMe.txt)to local IIS server folder(IISroot/upnp/FileStore).
IISroot/upnp/FileStore folder has set "write" priviledge and use anonymous access. Additionally, this folder also configure "application mapping"(ext file name: .txt, exe file path: SystemRoot\api.dll, verb: all)
IIS run in Windows XP SP3.
 
I use WinInet API to write following code to upload ReadMe.txt(lcoal file) to /upnp/FileStore(IIS folder). All APIs has no error(e.g. NULL handle or return FALSE), but finally ReadMe.txt is not generated in desired IIS folder, and IIS server return http status 405(Method not allowed). I don't know why, and hope someone can help me.
 
BOOL InternetPutFile(PNET_PUTFILE_PARA pNetPutFilePara)
{
DWORD dwFlags;
HINTERNET hOpen = NULL, hConnect = NULL;
INTERNET_BUFFERS BufferIn = {0};
DWORD dwBytesRead;
DWORD dwBytesWritten;
BYTE pBuffer[1024]; // Read from file in 1K chunks
BOOL bRead, bRet;
CString strAgent = "Finder";
 
BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS );
 
InternetGetConnectedState(&dwFlags, 0);
if(!(dwFlags & INTERNET_CONNECTION_PROXY))
hOpen = InternetOpen(strAgent, INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY, NULL, NULL, 0);
else
hOpen = InternetOpen(strAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if(!hOpen)
{
::MessageBox(pNetPutFilePara->hParentWnd, "Internet connect error!", MsgTitle, MB_OK);
}
 
hConnect = InternetConnect(hOpen, "locahost", INTERNET_DEFAULT_HTTP_PORT, pNetPutFilePara->szUserName, pNetPutFilePara->szPassWord, INTERNET_SERVICE_HTTP, 0, 0);
 
HINTERNET hRequest = HttpOpenRequest (hConnect, "PUT",
"/upnp/FileStore/ReadMe.txt", NULL, NULL, NULL, 0, 0);
if (!hRequest)
{
printf("Failed to open request handle: %lu\n", GetLastError ());
return FALSE;
}
 
HANDLE hFile = CreateFile (pNetPutFilePara->szLocalFilePath, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
printf("\nFailed to open local file %s.", pNetPutFilePara->szLocalFilePath);
return FALSE;
}
 
BufferIn.dwBufferTotal = GetFileSize (hFile, NULL);
printf ("File size is %d\n", BufferIn.dwBufferTotal );
 
if(!HttpSendRequestEx( hRequest, &BufferIn, NULL, HSR_INITIATE, 0))
{
printf( "Error on HttpSendRequestEx %lu\n",GetLastError() );
return FALSE;
}
 
DWORD sum = 0;
do
{
if (!(bRead = ReadFile (hFile, pBuffer, sizeof(pBuffer),
&dwBytesRead, NULL)))
{
printf ("\nReadFile failed on buffer %lu.",GetLastError());
break;
}
if (!(bRet=InternetWriteFile( hRequest, pBuffer, dwBytesRead,
&dwBytesWritten)))
{
printf ("\nInternetWriteFile failed %lu", GetLastError());
break;
}
sum += dwBytesWritten;
}
while (dwBytesRead == sizeof(pBuffer)) ;
 
CloseHandle (hFile);
printf ("Actual written bytes: %d\n", sum);
 
if(!HttpEndRequest(hRequest, NULL, 0, 0))
{
printf( "Error on HttpEndRequest %lu \n", GetLastError());
return FALSE;
}
return TRUE;
}
Posted 2-Jan-10 6:39am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I think you have to call function "HttpAddRequestHeaders" to mention the content type. It may be like the following:
 
HttpAddRequestHeaders( hConnect, "Content-Type: multipart/form-data; boundary=----------aaaaaa", dwHeadersLength, dwModifiers );
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 7,105
1 DamithSL 5,079
2 Maciej Los 4,866
3 Sergey Alexandrovich Kryukov 4,617
4 Kornfeld Eliyahu Peter 4,384


Advertise | Privacy | Mobile
Web03 | 2.8.141223.1 | Last Updated 13 Jan 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100