我正在开发一个必须与JSON / REST服务交互的小应用程序。 在我的c#应用程序中与它进行交互的最简单方法是什么。
我不需要拥有最好的表现,因为它只是一种可以每天进行一次同步的工具,我更倾向于易用性和开发时间。
(有问题的服务将是我们当地的JIRA实例)。
答案 0 :(得分:-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();