简体   繁体   English

C#使用RestSharp删除获取响应405

[英]C# Getting Response 405 by using RestSharp Delete

I try to delete a configuration by calling an Delete API. 我尝试通过调用Delete API删除配置。 For my API requests I use Restsharp because it works fine with GET and POST. 对于我的API请求,我使用Restsharp,因为它可以与GET和POST配合使用。

Now i have following problem. 现在我有以下问题。 When I try to call delete I get Error 当我尝试致电Delete时出现错误

405 "MethodNotAllowed" . 405“ MethodNotAllowed”

In my response I have following message: 在我的回复中,我有以下消息:

"HTTP method not allowed, supported methods: GET" “不允许使用HTTP方法,支持的方法:GET”

Code snipet: 代码片段:

var client = new RestClient("http://test.de/");
var request = new RestRequest("testrules/test");
request.Credentials = new NetworkCredential("username", "password");
var response = client.Delete(request);

I also tried the client.Execute() method and adding the id of the element which I want to delete to set over AddParameter() and AddUrlSegment() methods. 我还尝试了client.Execute()方法,并添加了要删除的元素的ID,以通过AddParameter()AddUrlSegment()方法进行设置。

I've tracked the response in Fiddler: 我在Fiddler中跟踪了响应:

DELETE http://test.de/testrules/test HTTP/1.1 
Accept: application/json, application/xml, text/json, text/x-json, text/javascript, text/xml 
User-Agent: RestSharp/105.0.1.0 
Host: test.de
Content-Length: 0
Accept-Encoding: gzip, deflate
Connection: Keep-Alive

When i try my request with Advanced RestClient from Chrome i get following request in Fiddler: 当我尝试使用Chrome的Advanced RestClient请求时,在Fiddler中收到以下请求:

DELETE http://test.de/testrules/test HTTP/1.1
Host: test.de
Connection: keep-alive
Authorization: Basic Q29udGludW91czp0ZXN0
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36
Origin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo
Content-Type: application/json
Accept: */*
Accept-Encoding: gzip, deflate, sdch
Accept-Language: de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4

Does anybody know a example for Delete with RestSharp? 有人知道使用RestSharp进行删除的示例吗? I've looked at this example from stackoverflow: RestSharp simple complete example and on the examples from the RestSharp documentation but nothing of this examples helped me. 我从stackoverflow看了以下示例: RestSharp简单的完整示例,以及RestSharp文档中的示例,但是这些示例对我没有帮助。

Do you have the Rest API service code that you can post? 您有可以发布的Rest API服务代码吗? From what you wrote, it feels like the restsharp client issues the Delete request using HTTP DELETE keyword while the Rest API endpoint accepts HTTP GET for the Delete method; 从您编写的内容来看,感觉就像restsharp客户端使用HTTP DELETE关键字发出Delete请求,而Rest API终结点接受Delete方法的HTTP GET。 but without the info about the server-side code it's hard to tell. 但是如果没有有关服务器端代码的信息,这很难说。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM