\"ProductName\":\"苹果BAK\"

时间:2021-07-28 07:37:58

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