API调用HttpClient

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

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