RestSharp 封状实例



1
public class Rest<T> 2 { 3 private static Logger logger = LogManager.GetCurrentClassLogger(); 4 private IRestClient client; 5 string proxyIp = AppConfig.proxyIp; 6 int proxyPort = AppConfig.proxyPort; 7 public Rest(string url) 8 { 9 10 client = new RestClient(url);
// 代理
11 client.Proxy = new WebProxy(proxyIp, proxyPort); 12 } 13 public async Task<T> RestExecute(RestRequest request) 14 { 15 try 16 {
  //HTTPS
17 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;20 //request.AddHeader("Content-Type", "application/json"); 21 IRestResponse<T> response = await client.Execute<T>(request); 22 logger.Debug("====== Rest response:" + JsonConvert.SerializeObject(response)); 23 return response.Data; 24 } 25 catch (Exception ex) 26 { 27 logger.Error("====== Rest error:" + ex.Message); 28 } 29 return default(T); 30 } 31 }

 

posted on 2019-05-15 10:33  #知了  阅读(853)  评论(0编辑  收藏  举报