这里列出了两种选择 HttpClient 异步提交
1.HttpClient:
public static string HttpClientPost(string url,string requestJson) { try { string result = string.Empty; Uri postUrl = new Uri(url); using (HttpContent httpContent = new StringContent(requestJson)) { httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); using (var httpClient = new HttpClient()) { httpClient.Timeout = new TimeSpan(0, 0, 60); result = httpClient.PostAsync(url, httpContent).Result.Content.ReadAsStringAsync().Result; } } return result; } catch (Exception e) { throw e; } }
对应接收数据:
[Route("api/[controller]/[action]")] [ApiController] [Authorize] public class WareHouseController : ControllerBase { [HttpPost] [AllowAnonymous] public IActionResult Test([FromBody]ModifyModel model) { try { } catch (Exception e) { } return new JsonResult( ); } }
第二种请求方式:
//var content = new StringContent(jsonContent, Encoding.UTF8, "application/json"); ////请求 //HttpResponseMessage response = httpClient.PostAsync(url, content).Result; //if (response.IsSuccessStatusCode) //{ // Task<string> t = response.Content.ReadAsStringAsync(); // if (t != null) // { // return t.Result; // } //} //return string.Empty;