RestSharp是一个轻量级HTTP客户端库,主要功能是通过HTTP对远程资源进行同步异步调用,可将请求主体序列化为JSON或XML并反序列化相应。
请求主体的方式:JSON、XML和表单数据
参数类型:查询、URL段、标头、cookie、正文
官方的例子如下:
using RestSharp;
using RestSharp.Authenticators;
var client = new RestClient("https://api.twitter.com/1.1") {
Authenticator = new HttpBasicAuthenticator("username", "password")
};
var request = new RestRequest("statuses/home_timeline.json");
var response = await client.GetAsync(request, cancellationToken);
实例代码:
RestClient client = new RestClient(urlFilterTra);
RestRequest request = new RestRequest(Method.POST);
client.Timeout = m_Timeout;
request.AddHeader("Content-Type", "application/json");
byte[] json_buf = Encoding.UTF8.GetBytes(jsonData);
jsonData = Encoding.UTF8.GetString(json_buf, 0, json_buf.Length);
request.AddParameter("application/json", jsonData, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
string resultStr = response.Content;
list_result.Clear();
//反序列化
FiltrationRcvInfo filteRcvInfo=JsonConvert.DeserializeObject<FiltrationRcvInfo>(resultStr);