且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用多个参数在C#中使用REST服务(7个参数)

更新时间:2023-10-06 13:29:28

当我看到参数放在一起的时候,我觉得很像我会看到SQL连接,你好!



这是来自RestSharp页面的示例



When I see parameters put together like that I think much like I would if I saw SQL concatenation, YUCK !

Here's the example from the RestSharp page

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();





例如,看一下'AddParameter'方法 - 当然,这一定更容易! ... RestSharp - 用于.NET的简单REST和HTTP客户端 [ ^ ]