当前位置: 首页 > 工具软件 > RestSharp > 使用案例 >

C# API RestSharp RestResponse RestClient RestRequest 使用实例

沈骞仕
2023-12-01

1.引用RestSharp.dll

2.创建API POST接口

    /// <summary>
    /// API 公用方法调用
    /// </summary>
   public static class APIRequest
    {
        
        //对接地址与密锁
        public static string GatewayUrl = System.Configuration.ConfigurationManager.AppSettings["GatewayUrl"];
        public static string AppID = System.Configuration.ConfigurationManager.AppSettings["AppID"];
        public static string AppSecret = System.Configuration.ConfigurationManager.AppSettings["AppSecret"];
        
        //记录日志
        private static Logger _logger = LogManager.GetCurrentClassLogger();

        public static string EncodeBase64(string code)
        {
            string encode = "";
            byte[] bytes = Encoding.UTF8.GetBytes(code);
            try
            {
                encode = Convert.ToBase64String(bytes);
            }
            catch
            {
                encode = code;
            }
            return encode;
        }

        /// <summary>
        /// 请求接口
        /// </summary>
        /// <param name="url"></param>
        /// <param name="appId"></param>
        /// <param name="jsonStr"></param>
        /// <returns></returns>
        public static MessageResult PushRequests( string url, string jsonStr)
        {
            _logger.Info("地址:"+ url + "调用api json:" + jsonStr);
            string resResult = string.Empty;
            try
            {
                RestResponse response = null;

                if (!string.IsNullOrEmpty(url)&& !string.IsNullOrEmpty(jsonStr))
                {
                    RestClient _restClient = new RestClient(GatewayUrl+url);
                    RestRequest req = new RestRequest(Method.POST);
                    string AuthStr = "Basic " + EncodeBase64(AppID + ":" + AppSecret);
                    req.AddHeader("Authorization", AuthStr);
                    req.AddHeader("appID", AppID);
                    req.AddParameter("application/json; charset=utf-8", jsonStr, ParameterType.RequestBody);
                    req.RequestFormat = DataFormat.Json;
                    response = (RestResponse)_restClient.Execute(req);
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        resResult = response.Content;
                       
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(response.Content))
                        {

                            _logger.Info("调用推送数据接口异常:" + response.Content);
                        }
                        else
                        {

                            _logger.Info("调用推送数据接口异常:" + response.ErrorMessage);
                        }
                        throw new Exception("调用推送数据接口异常");
                    }

                }


            }
            catch (Exception ex)
            {

                Log.Instance.Info("推送数据错误:" + ex);
            
            }

            return JsonConvert.DeserializeObject<MessageResult>(resResult);
             
        }

        

       
    }

3.创建接口接收信息 MessageResul类

  /// <summary>
    /// API 返回结果
    /// </summary>
    public class MessageResult
    {
        public int Code { get; set; }
        public string Message { get; set; }
        public object Data { get; set; }

    }

 类似资料: