开发者

HTTP DELETE request

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."

Also, 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. You should remove Content-Type header which is useless here.
  2. Check if you are using IIS and if files/ folder that you are trying to delete has rights to delete. These are the user that I can think of: The IUSR or IUSR_MachineName account. The IIS_IUSRS or IIS_WPG group.
  3. For 501 error return - PUT, DELETE , OPTIONS are not enabled by default. Hence, you need to enable at web server level.

You should make sure that the following configurations are there in the config file. You can also see some other post related with delete. (modify below settings that suites your environment).

 <handlers>
      <remove name="WebDAV" />
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>


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...)


The following code worked for me, it might be of some use for you too.

string headername = "TokenName";
string headervalue = "0000000000";

var request = (HttpWebRequest)WebRequest.Create("https://URL");

request.Method = "DELETE";
request.Headers.Add(headername, headervalue);

try
{
    var response = (HttpWebResponse)request.GetResponse();

    var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

    var jss = new JavaScriptSerializer();
    var dict = jss.Deserialize<dynamic>(responseString);
    string message += "deleted Item with id" + dict["id"];
}
catch
{
    string message += "Didn't delete Item";
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜