Click here to Skip to main content

Vincent_Kao asked:

Open original thread
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;
}
Tags: VC8.0

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).



Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 26 Mar 2009
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