RestSharp 封状实例

本文介绍了一种在C#中使用RESTful API进行数据交换的方法,特别关注了通过代理服务器发送请求的过程。文章展示了如何创建RestClient实例,并设置WebProxy来通过指定的代理服务器进行HTTPS请求。此外,还提供了错误处理和日志记录的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



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 }

 

转载于:https://www.cnblogs.com/cicada/p/10867968.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值