码迷,mamicode.com
首页 > Windows程序 > 详细

API调用HttpClient

时间:2017-11-22 22:09:23      阅读:483      评论:0      收藏:0      [点我收藏+]

标签:add   name   message   ica   gas   delete   tpc   oca   ase   

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web;
using System.Web.Mvc;
using System.Net.Http.Headers;
using WebapiClient.Models;

namespace WebapiClient.Controllers
{
public class HomeController : Controller
{
public ActionResult Client_Get()
{
//定义服务器地址
Uri url = new Uri("http://localhost:63649");
//创建接口
HttpClient client = new HttpClient();// System.Net.Http
client.BaseAddress = url;
//设置报头 System.Net.Http.Headers
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//返回
HttpResponseMessage message= client.GetAsync("api/Product").Result;
//判断返回的响应信息是否是成功的
if (message.IsSuccessStatusCode)
{
ViewBag.Mess = message.Content.ReadAsStringAsync().Result;
}
client.Dispose();//释放资源
return View("Index");
}

public ActionResult Client_POST()
{
//定义服务器地址
Uri uri = new Uri("http://localhost:63649");
//创建接口
HttpClient httpClient = new HttpClient();
httpClient.BaseAddress = uri;
//设置报头
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//htpcontent 去传递一个对象 对象格式是json的
HttpContent httpContent = new StringContent("{\"ProductID\":4,\"ProductName\":\"新苹果\",\"Content\":\"新到的烟台的苹果\"}");
httpContent.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/json");
//返回
HttpResponseMessage response = httpClient.PostAsync("api/Product", httpContent).Result;
//判断返回的响应信息是否是成功的
if (response.IsSuccessStatusCode)
{
ViewBag.resultStr = response.Content.ReadAsStringAsync().Result;
}
else
{
ViewBag.resultStr = "请求错误";
}
httpClient.Dispose();

return View("Index");
}

public ActionResult Client_PUT()
{
//定义服务器地址
Uri uri = new Uri("http://localhost:63649");
//创建接口
HttpClient httpClient = new HttpClient();
httpClient.BaseAddress = uri;
//设置报头
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//返回
HttpContent httpContent = new StringContent("{\"ProductID\":1,\"ProductName\":\"苹果BAK\",\"Content\":\"烟台的苹果BAK\"}");
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
HttpResponseMessage response = httpClient.PutAsync("/api/Product/1", httpContent).Result;
//判断返回的响应信息是否是成功的
if (response.IsSuccessStatusCode)
{
ViewBag.resultStr = response.Content.ReadAsStringAsync().Result;
}
else
{
ViewBag.resultStr = "请求错误";
}
httpClient.Dispose();

return View("Index");
}

public ActionResult Client_DELETE()
{
//定义服务器地址
Uri uri = new Uri("http://localhost:63649");
//创建接口
HttpClient httpClient = new HttpClient();
httpClient.BaseAddress = uri;
//设置报头
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//返回
HttpResponseMessage response = httpClient.DeleteAsync("/api/Product/1").Result;
//判断返回的响应信息是否是成功的
if (response.IsSuccessStatusCode)
{
ViewBag.resultStr = response.Content.ReadAsStringAsync().Result;
}
else
{
ViewBag.resultStr = "请求错误";
}
httpClient.Dispose();

return View("Index");
}
}
}

API调用HttpClient

标签:add   name   message   ica   gas   delete   tpc   oca   ase   

原文地址:http://www.cnblogs.com/htbmvc/p/7880859.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!