Search code examples
c#httpwebrequestwebrequest

The remote server returned an error: (405) Method Not Allowed


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.


Solution

  • I need to add a webpage on the server to handle the uploading.