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