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

  • 相关阅读:
    ArrayList和Vector的区别?HashMap和Hashtable的区别?
    试题:关键字public, private, protected的区别?以及不写时默认是什么?
    试题:用JavaScript实现密码验证功能
    RPC和RMI的区别(Difference Between RPC and RMI)
    js中从blob提取二进制
    netty 3.9.2 UDP协议服务器和客户端DEMO
    Java NIO的多路复用及reactor
    android屏蔽home键的实现
    搜索引擎对相似图片搜索识别的原理(一)
    代理模式(设计模式)
  • 原文地址:https://www.cnblogs.com/htbmvc/p/7880859.html
Copyright © 2020-2023  润新知