一、简介
最近,在做一个数据传输的服务,我在一个Worker Service里面需要访问 WebAPI 接口,并传输数据,也可以提交数据。由于第一次使用 RestSharp 发送请求,也遇到了很多问题,不过最终把问题都解决了。什么东西都一样,第一次做都会才很多坑。
今天,我就两个小的知识点,就是通过使用 RestSharp 访问 WebAPI,提交 FromBody 和 FromForm 两种方式的数据,还是有些区别的。
我当初也搞了一段时间。
我先说一下我的编码环境:
操作系统:Windows 10 Professional
开发工具:Visual Studio 2022
客户端程序:Worker Service
服务端程序:Net Core WebAPI
框架版本:Net7.0
二、我的代码
以下我就介绍一下,使用 RestSharp、以FromBody 和 FromForm 两种方式提交数据的实现,废话不多说,直接上代码。如果是重点代码,我会以红色标注。
WebAPI 端的代码:
[HttpPost] public async Task<CommonJsonResult<string>> AddOrUpdateScannerSetting([FromBody]/[FromForm] ScannerSettingInputDto inputDto) { }
其他的代码不用细看,都是我的逻辑代码。
Worker Service 端的代码:
using (RestClient restClient = new RestClient()) { var updateScannerSettingRequest = new RestRequest(new Uri(updateScannerSettingUrl), Method.Post); updateScannerSettingRequest.AddHeader("Authorization", "Bearer " + token);//增加的 JWT 认证 updateScannerSettingRequest.AddHeader("Accept", "application/json"); updateScannerSettingRequest.RequestFormat = DataFormat.Json; #region [FromBody]接收,这里有两种方式,都是可以的 1、updateScannerSettingRequest.AddParameter("application/json", JsonConvert.SerializeObject(scannerSettingInputDto), ParameterType.RequestBody); 2、updateScannerSettingRequest.AddBody(scannerSettingInputDto); #endregion #region [FromForm]接收,所有字段都需要自己添加 updateScannerSettingRequest.AddParameter("ScannerId", scannerSettingInputDto.ScannerId); updateScannerSettingRequest.AddParameter("Camera1Id", scannerSettingInputDto.Camera1Id); updateScannerSettingRequest.AddParameter("Camera2Id", scannerSettingInputDto.Camera2Id); updateScannerSettingRequest.AddParameter("Camera3Id", scannerSettingInputDto.Camera3Id); updateScannerSettingRequest.AddParameter("Camera4Id", scannerSettingInputDto.Camera4Id); updateScannerSettingRequest.AddParameter("Camera1IntegrationTime", scannerSettingInputDto.Camera1IntegrationTime); updateScannerSettingRequest.AddParameter("Camera2IntegrationTime", scannerSettingInputDto.Camera2IntegrationTime); updateScannerSettingRequest.AddParameter("Camera3IntegrationTime", scannerSettingInputDto.Camera3IntegrationTime); updateScannerSettingRequest.AddParameter("Camera4IntegrationTime", scannerSettingInputDto.Camera4IntegrationTime); updateScannerSettingRequest.AddParameter("Camera1Image", scannerSettingInputDto.Camera1Image); updateScannerSettingRequest.AddParameter("Camera2Image", scannerSettingInputDto.Camera2Image); updateScannerSettingRequest.AddParameter("Camera3Image", scannerSettingInputDto.Camera3Image); updateScannerSettingRequest.AddParameter("Camera4Image", scannerSettingInputDto.Camera4Image); updateScannerSettingRequest.AddParameter("ScannerCoordinate", scannerSettingInputDto.ScannerCoordinate); updateScannerSettingRequest.AddParameter("PatrolFile", scannerSettingInputDto.PatrolFile); updateScannerSettingRequest.AddParameter("WarningPhone", scannerSettingInputDto.WarningPhone); updateScannerSettingRequest.AddParameter("IsUpdate", scannerSettingInputDto.IsUpdate); updateScannerSettingRequest.AddParameter("Remarks", scannerSettingInputDto.Remarks); #endregion var updateScannerSettingResponse = await restClient.ExecuteAsync(updateScannerSettingRequest); var updateScannerSettingContent = updateScannerSettingResponse.Content; if (!string.IsNullOrEmpty(updateScannerSettingContent) && !string.IsNullOrWhiteSpace(updateScannerSettingContent)) { var commontResultObject = JsonConvert.DeserializeObject<CommonJsonResult<string>>(updateScannerSettingContent); if (commontResultObject != null && commontResultObject.Success) { result = true; } else { result = false; _logger.LogError(updateScannerSettingContent); } } else { result = true; } }
还有一个辅助类型:
/// <summary> /// 该类型定义了以 JSON 为返回结果的通用数据格式。 /// </summary> /// <typeparam name="TResult">结果的返回类型。</typeparam> public sealed class CommonJsonResult<TResult> { /// <summary> /// 获取或者设置操作的结果是成功还是失败,True 表示操作成功,False 表示操作失败。 /// </summary> public bool Success { get; set; } /// <summary> /// 获取或者设置操作的结果编码值。 /// </summary> public int StateCode { get; set; } /// <summary> /// 获取或者设置操作的提示信息。 /// </summary> public string? Message { get; set; } /// <summary> /// 获取或者设置数据的结果。 /// </summary> public TResult? DataResult { get; set; } }