postman 跟restsharp 模拟请求http

时间:2019-12-12 11:37:28   收藏:0   阅读:1973

https://github.com/restsharp/RestSharp

 

postman 生成的访问代码;

好用!

 

Features

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

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

// add parameters for all properties on an object 为对象上的所有属性添加参数
request.AddObject(@object);

// or just whitelisted properties  或者为某个属性添加参数
request.AddObject(object, "PersonId", "Name", ...);

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

// add files to upload (works with compatible verbs)  上传文件
request.AddFile("file", path);

// execute the request   执行请求
var response = client.Post(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();
var response2 = client.Post<Person>(request);
var name = response2.Data.Name;

// or download and save file to disk  
client.DownloadData(request).SaveAs(path);

// easy async support
await client.ExecuteTaskAsync(request);

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

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

原文:https://www.cnblogs.com/zxs-onestar/p/12027747.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!