您的位置:首页 > 理论基础 > 计算机网络

.NET MVC4 用HttpClient后台post 请求webapi

2015-10-27 11:20 549 查看
视图控制器后台代码

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();
}


API控制器后台代码

// POST api/values
public HttpResponseMessage Post([FromBody]string value)
{
return Request.CreateResponse(HttpStatusCode.OK, value);
}


经过实测,是好用的!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: