2016-11-08 48 views
-4

我正在开发一个必须与JSON/REST服务进行交互的小应用程序。 在我的c#应用程序中与它交互的最简单的选项是什么?如何与C#中的REST服务进行交互

我不需要有最好的表现,因为它只是一个工具,每天会进行一次同步,我更倾向于易用性和开发时间。

(该服务将成为我们当地的JIRA实例)。

+1

这主要是基于意见,并要求框架的建议。两者都是无关紧要的。 – mason

+0

?我不要求任何框架,只是怎么做 – J4N

+1

你会认为是6年的成员;前6%; 〜4500代表会知道如何提出一个好问题_? – MickyD

回答

-1

我认为目前最好的方法是使用RestSharp。这是一个免费的Nuget包,你可以参考。这是很容易使用,这是他们的网站的例子:

var client = new RestClient("http://example.com"); 
// client.Authenticator = new HttpBasicAuthenticator(username, password); 

var request = new RestRequest("resource/{id}", Method.POST); 
request.AddParameter("name", "value"); // adds to POST or URL querystring based on Method 
request.AddUrlSegment("id", "123"); // replaces matching token in request.Resource 

// easily add HTTP Headers 
request.AddHeader("header", "value"); 

// add files to upload (works with compatible verbs) 
request.AddFile(path); 

// execute the request 
IRestResponse response = client.Execute(request); 
var content = response.Content; // raw content as string 

// or automatically deserialize result 
// return content type is sniffed but can be explicitly set via RestClient.AddHandler(); 
RestResponse<Person> response2 = client.Execute<Person>(request); 
var name = response2.Data.Name; 

// easy async support 
client.ExecuteAsync(request, response => { 
    Console.WriteLine(response.Content); 
}); 

// async with deserialization 
var asyncHandle = client.ExecuteAsync<Person>(request, response => { 
    Console.WriteLine(response.Data.Name); 
}); 

// abort the request on demand 
asyncHandle.Abort();