Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

1 solution

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 );
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900