Click here to Skip to main content
15,885,278 members

Use WinInet to upload a file to http server. Http server return status 405

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: C++, Visual C++ 8.0

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

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.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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