I want to use HttpWebRequest to post a file to the server:
private void testUpload()
{
FileStream source = File.Open(@"C:\test.txt", FileMode.Open);
var request =
(HttpWebRequest)WebRequest.Create(new Uri("http://example.com/Project/"));
request.Method = "POST";
request.BeginGetResponse(DataUploadCompleted, request);
}
private void DataUploadCompleted(IAsyncResult ar)
{
var request = (HttpWebRequest)ar.AsyncState;
var response = request.EndGetResponse(ar);
}
I got this exception:
The remote server returned an error: (405) Method Not Allowed.
When I access: "http://example.com/Project/", the page shows:
Directory Listing Denied
This Virtual Directory does not allow contents to be listed.
However, I already chmod 777 for the folder: project and allow IIS user to upload files on it (full permission).
Why I got that exception?
I searched for a solution. Some people advices to use:
NetworkCredential myCred = new NetworkCredential("myusername", "mypassword");
request.Credentials = myCred;
Is myusername and mypassword the account of the FTP?
If I have to use FTP account, then I don't like that. Can I use some other credentials rather then FTP account? Because I don't want to give the ftp account and people will access on my server.
go to IIS and then open directory browsing and enable it, this should work
I need to add a webpage on the server to handle the uploading.
Sometimes this error for IIS config in the server you want "GetResponse();".
For preventing attack the server by lots of request to Web Api , IIS check user-agent and when there is not user-agent Server return 405 error.
You must set user-agent for example browser user-agent for passing this error.
see this code and use it in your code :