RestSharp简单完整的例子[关闭]RestSharp简单完整的例子[关闭](RestSharp

2019-05-13 13:50发布

我一直在试图创建一个使用RestSharp调用REST API一个简单的原型Web应用程序。

我已经无法找到它的一个很好的例子。 任何人都可以请分享,并告诉我到正确的资源吗? 我已经看过之后,并没有提供什么我要找的,即功能齐全的例子:

http://restsharp.org/ (不具有充分例如应用程序)

http://www.stum.de/2009/12/22/using-restsharp-to-consume-restful-web-services/ (好像是老)

虽然原型,我得到下面的下面的代码错误:

RestResponse response = client.Execute(request);

*Cannot implicitly convert type 'IRestResponse' to 'RestResponse'. An explicit conversion exists (are you missing a cast?)  *

Answer 1:

我设法找到关于这个问题,哪个环节断,以实现RestSharp一个开源项目博客文章。 希望的对你有所帮助。

http://dkdevelopment.net/2010/05/18/dropbox-api-and-restsharp-for-ac-developer/博客文章是2舞伴,而该项目是在这里: https://github.com/ dkarzon / DropNet

如果你有什么不工作一个完整的示例这可能会有帮助。 这是很难得到客户的设置方式,如果你不提供代码上下文。



Answer 2:

Pawel Sawicz .NET blog has a real good explanation and example code, explaining how to call the library;

GET:

var client = new RestClient("192.168.0.1");
var request = new RestRequest("api/item/", Method.GET);
var queryResult = client.Execute<List<Items>>(request).Data;

POST:

var client = new RestClient("http://192.168.0.1");
var request = new RestRequest("api/item/", Method.POST);
request.RequestFormat = DataFormat.Json;
request.AddBody(new Item
{
ItemName = someName,
Price = 19.99
});
client.Execute(request);

DELETE:

var item = new Item(){//body};
var client = new RestClient("http://192.168.0.1");
var request = new RestRequest("api/item/{id}", Method.DELETE);
request.AddParameter("id", idItem);

client.Execute(request)

The RestSharp GitHub page has quite an exhaustive sample halfway down the page. To get started install the RestSharp NuGet package in your project, then include the necessary namespace references in your code, then above code should work (possibly negating your need for a full example application).



Answer 3:

更改

RestResponse response = client.Execute(request);

IRestResponse response = client.Execute(request);

为我工作。



文章来源: RestSharp simple complete example [closed]