C# RestSharp 简单完整示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10226089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 12:57:45  来源:igfitidea点击:

RestSharp simple complete example

c#restrestsharp

提问by Nil Pun

I've been trying to create a simple prototype web application that uses RestSharp to call Rest API.

我一直在尝试创建一个简单的原型 Web 应用程序,它使用 RestSharp 来调用 Rest API。

I've not been able to find one good example of it. Could anyone please share and direct me to right resource please? I've already looked at following, and doesn't provide what I'm looking for i.e fully functional example:

我一直没能找到一个很好的例子。任何人都可以分享并指导我找到正确的资源吗?我已经看过以下内容,但没有提供我正在寻找的内容,即功能齐全的示例:

http://restsharp.org/(Doesn't have full application with example)

http://restsharp.org/(没有完整的应用示例)

http://www.stum.de/2009/12/22/using-restsharp-to-consume-restful-web-services/(seems to be old)

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

While prototyping I get the error below for code below:

在进行原型设计时,我收到以下代码的错误:

RestResponse response = client.Execute(request);

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

采纳答案by pms1969

I managed to find a blog post on the subject, which links off to an open source project that implements RestSharp. Hopefully of some help to you.

我设法找到了有关该主题的博客文章,该文章链接到一个实现 RestSharp 的开源项目。希望对你有所帮助。

http://dkdevelopment.net/2010/05/18/dropbox-api-and-restsharp-for-a-c-developer/The blog post is a 2 parter, and the project is here: https://github.com/dkarzon/DropNet

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

It might help if you had a full example of what wasn't working. It's difficult to get context on how the client was set up if you don't provide the code.

如果你有一个关于什么不起作用的完整例子,它可能会有所帮助。如果您不提供代码,就很难了解客户端是如何设置的。

回答by fractal

Changing

改变

RestResponse response = client.Execute(request);

to

IRestResponse response = client.Execute(request);

worked for me.

为我工作。

回答by wonea

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

Pawel Sawicz .NET blog有很好的解释和示例代码,解释了如何调用库;

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

RestSharp GitHub的页面有相当详尽的样本网页下半部。要开始在您的项目中安装 RestSharp NuGet 包,然后在您的代码中包含必要的命名空间引用,那么上面的代码应该可以工作(可能不需要完整的示例应用程序)。

NuGet RestSharp

NuGet RestSharp