.NET MVC4 用HttpClient后台post 请求webapi

  • 转载:https://blog.csdn.net/Anlan2010/article/details/49445905

  • public ActionResult Index()

  • {

  • var requestJson = JsonConvert.SerializeObject("[{'CityId':18,'CityName':'西安','ProvinceId':27,'CityOrder':1},{'CityId':53,'CityName':'广州','ProvinceId':27,'CityOrder':1}]");

  • HttpContent httpContent = new StringContent(requestJson);

  • httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

  • var httpClient = new HttpClient();

  • var responseJson = httpClient.PostAsync("http://localhost:6972/api/test/Post/", httpContent)

  • .Result.Content.ReadAsStringAsync().Result;

  •  
  • ViewBag.s = responseJson;

  • return View();

  • }

  •