欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 时评 > RestSharp和Newtonsoft.Json结合发送和解析http

RestSharp和Newtonsoft.Json结合发送和解析http

2025/4/23 1:13:49 来源:https://blog.csdn.net/m0_72613306/article/details/147305545  浏览:    关键词:RestSharp和Newtonsoft.Json结合发送和解析http

1.下载RestSharp和Newtonsoft.Json

image-20250417140927406

2编写ApiRequest和ApiResponse和调用工具类HttpRestClient

image-20250417141017271

请求模型

  /// <summary>/// 请求模型/// </summary>public class ApiRequest{/// <summary>/// 请求地址/api路由地址/// </summary>public string Route { get; set; }​/// <summary>/// 请求方式(Post,Get,Delete,Put)/// </summary>public Method Method { get; set; }​/// <summary>/// 请求参数/// </summary>public object Parameters {  get; set; }​/// <summary>/// 发送的数据类型(默认是json)/// </summary>public string ContentType { get; set; } = "application/json";}

接收模型

 /// <summary>/// 接收模型/// </summary>public class ApiResponse{/// <summary>/// 结果编码/// </summary>public int ResultCode { get; set; }​/// <summary>/// 结果信息/// </summary>public string Msg { get; set; }​/// <summary>/// 数据/// </summary>public object ResultData { get; set; }}

http调用工具类,要对应后端接口把baseUrl替换掉

 /// <summary>/// 调用api工具类/// </summary>public class HttpRestClient{private readonly RestClient Client;​private readonly string baseUrl = "http://localhost:10036/api/";public HttpRestClient(){Client = new RestClient();}/// <summary>/// 请求/// </summary>/// <param name="request">请求数据</param>/// <returns>接收数据</returns>public ApiResponse Execute(ApiRequest apiRequest){//请求方式RestRequest request = new RestRequest(apiRequest.Method);//请求内容request.AddHeader("Content-Type", apiRequest.ContentType);if (apiRequest.Parameters != null){request.AddParameter("param",JsonConvert.SerializeObject(apiRequest.Parameters),ParameterType.RequestBody);}​//请求地址Client.BaseUrl = new Uri(baseUrl + apiRequest.Route);​var res=Client.Execute(request);​//请求成功if (res.StatusCode == System.Net.HttpStatusCode.OK){//把json格式反序列化成对象return JsonConvert.DeserializeObject<ApiResponse>(res.Content);}//失败else{return new ApiResponse { ResultCode = -99, Msg = "服务器忙,请稍后" };}}}

3.Prism依赖注入HttpRestClient

  public partial class App : PrismApplication{protected override Window CreateShell(){//设置启动页return Container.Resolve<MainWin>();}​/// <summary>/// 依赖注入/// </summary>/// <param name="containerRegistry"></param>protected override void RegisterTypes(IContainerRegistry containerRegistry){containerRegistry.GetContainer().Register<HttpRestClient>(made: Parameters.Of.Type<string>(serviceKey: "webUrl"));​}​​}

4.用一个登录例子说明

 private void Login(){if (string.IsNullOrEmpty(LoginAccountDTO.Account) || string.IsNullOrEmpty(LoginAccountDTO.Password)){//MessageBox.Show("账号或密码未输入");aggregator.GetEvent<MsgEvent>().Publish("账号或密码未输入");return;}​//创建请求ApiRequest apiRequest = new ApiRequest();apiRequest.Method = RestSharp.Method.GET;LoginAccountDTO.Password=Md5Helper.GetMd5(LoginAccountDTO.Password);apiRequest.Route = $"Account/Login?account={LoginAccountDTO.Account}&password={LoginAccountDTO.Password}";ApiResponse response = httpRestClient.Execute(apiRequest);​//登陆成功if (response.ResultCode == 1){//反序列化jsonLoginAccountDTO loginAccountDTO = JsonConvert.DeserializeObject<LoginAccountDTO>(response.ResultData.ToString());DialogParameters parameters = new DialogParameters();parameters.Add("LoginName", loginAccountDTO.Name);RequestClose(new DialogResult(ButtonResult.OK, parameters));}//登陆失败else{//MessageBox.Show(response.Msg);aggregator.GetEvent<MsgEvent>().Publish(response.Msg);}​}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词