tags:

views:

50

answers:

2

I'm attempting to do an HTTP DELETE in C# from my code behind and am unable to do this. After looking at the members of the WebRequestMethods.Http type, i'm not even sure that this is possible.

Here is my code:

try
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost/NameFiles/00000.txt");
    request.Method = "DELETE";
    request.ContentType = "application/x-www-form-urlencoded";
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();

    if (response.StatusCode == HttpStatusCode.OK)
    {
        // some code
    }
}
catch (Exception ex)
{
    ex.ToString();
}

Running this from my development environment I get: "The remote server returned an error: (401) Unauthorized."
I have received a different result on a server that I assume has something to do with settings in IIS: "The remote server returned an error: (501) Not Implemented."

Any Ideas?

UPDATE:
As I mentioned in a comment to an answer below, I am able to send DELETE requests from a classic asp page using vbscript on the same server to the same location as the request from my aspx page using c#. Why would these be different?

+1  A: 

Found this article for you, should explain your issues.

http://www.eggheadcafe.com/software/aspnet/29818500/iis-60-501-error--delete.aspx

El Guapo
I needed to enable WebDAV in the Web Service Extensions. To resolve the ensuing 401 error, I also needed to give Scripts and Executables execute permission to the virtual directory I was using.
Jeremy Cron
A: 

401 means that the server expects you to authenticate (maybe it allows anonymous access only for read operations)?

Besides, you may want to remove the Content-Type header unless you really plan to send a body with the request (which would be unorthodox for DELETE...)

Julian Reschke
I'll try to figure out if I need to authenticate. Any idea what the difference would be as to why I am able to send DELETE requests with classic asp/vbscript and not my aspx/csharp? I'm not authenticating with either.
Jeremy Cron
Julian Reschke