【发布时间】:2012-09-04 19:29:35
【问题描述】:
我从控制台应用程序调用了一个简单的 Restful 服务,所以我使用 WebClient。我想知道这个删除的调用是否正确。
网址看起来像localhost/RestService1/Person/1
using (var client = new WebClient())
{
client.UploadString(url, "DELETE", "");
}
我不喜欢 UploadString 在没有 data 参数的情况下没有重载。传递一个空参数并不适合我。有没有更好的方法用于DELETE?
我可以使用WebRequest,但我只想使用WebClient 保持一致。
这是WebRequest 块
var request = WebRequest.Create(url);
request.Method = "DELETE";
var response = (HttpWebResponse)request.GetResponse();
两个块都可以正常工作,但最好的是什么?还是有更好的办法?
【问题讨论】:
-
除了那些引用中与 DELETE 和 RESTful 的关系之外,我认为
WebClient并没有真正为您提供 DELETE 的语义。Webclient只是在幕后使用WebRequest(HttpWebRequest),所以我认为直接使用HttpWebRequest) 更具可读性。 -
我不认为前 2 个链接涉及我的问题。我的 url 是 localhost/RestService1/Person/1 其中 /1 是人员 ID,所以它很安静。我做了更多的挖掘,发现 WebClient 主要是一个包装器。谢谢
标签: c# httpwebrequest webclient