ToB企服应用市场:ToB评测及商务社交产业平台

标题: WPF怎么通过RestSharp向后端发请求 [打印本页]

作者: 农民    时间: 2024-11-4 10:19
标题: WPF怎么通过RestSharp向后端发请求
1.下载RestSharpNuGet包

2.请求类和响应类
  1. public class ApiRequest
  2. {
  3.     /// <summary>
  4.     /// 请求地址
  5.     /// </summary>
  6.     public string Route { get; set; }
  7.     /// <summary>
  8.     /// 请求方式
  9.     /// </summary>
  10.     public Method Method { get; set; }
  11.     /// <summary>
  12.     /// 请求参数
  13.     /// </summary>
  14.     public Object Parameters { get; set; }
  15.     /// <summary>
  16.     /// 请求头,表示发送的请求的类型、数据格式等
  17.     /// </summary>
  18.     public string ContentType { get; set; } = "application/json";
  19. }
复制代码
  1. public class ApiResponse
  2. {
  3.     public int Code { get; set; }
  4.     public string Msg { get; set; }
  5.     public Object Data { get; set; }
  6. }
复制代码
3.封装请求工具类
  1. public class HttpClient
  2. {
  3.      private readonly RestClient Client; // 客户端
  4.      private readonly string baseUrl = "http://localhost:63615/api/";
  5.      public HttpClient()
  6.      {
  7.          Client = new RestClient();
  8.      }
  9.      /// <summary>
  10.      /// 执行请求
  11.      /// </summary>
  12.      /// <param name="request">请求数据</param>
  13.      /// <returns>响应数据</returns>
  14.      public ApiResponse Execute(ApiRequest ApiRequest)
  15.      {
  16.          var request = new RestRequest(ApiRequest.Method);
  17.          request.AddHeader("Content-Type", ApiRequest.ContentType);
  18.          if (ApiRequest.Parameters!= null)
  19.          {
  20.              // SerializeObject 序列化参数 object -> json
  21.              request.AddParameter("params", JsonConvert.SerializeObject(ApiRequest.Parameters), ParameterType.RequestBody);
  22.          }
  23.          // 设置请求地址
  24.          Client.BaseUrl = new Uri(baseUrl + ApiRequest.Route);
  25.          // 发送请求
  26.          var res = Client.Execute(request);
  27.          if (res.StatusCode == System.Net.HttpStatusCode.OK)
  28.          {
  29.              // 反序列化响应数据 json -> object
  30.              return JsonConvert.DeserializeObject<ApiResponse>(res.Content);
  31.          }
  32.          else
  33.          {
  34.              return new ApiResponse { Code = (int)res.StatusCode, Msg = res.ErrorMessage };
  35.          }
  36.      }
  37. }
复制代码
4. 在接口中引入该类,并在构造函数中初始化
  1. private readonly IEventAggregator _eventAggregator;
复制代码
  1. public LoginUCViewModel(HttpClient httpClient)
  2. {
  3.     //请求Client
  4.     _httpClient = httpClient;
  5. }
复制代码
5.发送请求
  1. // 发送请求
  2. ApiRequest apiRequest = new ApiRequest();
  3. // 请求方式
  4. apiRequest.Method = RestSharp.Method.POST;
  5. // 请求地址(这里只写这一段是因为定义的时候定义了baseUrl
  6. apiRequest.Route = "Account/Register";
  7. AccountInfoDto.Password = Md5Helper.GetMd5(AccountInfoDto.Password);
  8. apiRequest.Parameters = AccountInfoDto;
  9. ApiResponse response = _httpClient.Execute(apiRequest);
复制代码
 到此,简单的请求封装就结束了

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4