zoukankan      html  css  js  c++  java
  • c# 【MVC】WebApi通过HttpClient来调用Web Api接口

    /// <summary>
    /// HttpClient实现Post请求(异步)
    /// </summary>
    static async void dooPost()
    {
    	string url = "http://localhost:52824/api/register";
    	 //设置HttpClientHandler的AutomaticDecompression
    	var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip };
    	//创建HttpClient(注意传入HttpClientHandler)
    	using (var http = new HttpClient(handler))
    	{
    		//使用FormUrlEncodedContent做HttpContent
    		var content = new FormUrlEncodedContent(new Dictionary<string, string>()       
    		{    {"Id","6"},
    			 {"Name","添加zzl"},
    			 {"Info", "添加动作"}//键名必须为空
    		 });
    
    		//await异步等待回应
    
    		var response = await http.PostAsync(url, content);
    		//确保HTTP成功状态值
    		response.EnsureSuccessStatusCode();
    		//await异步读取最后的JSON(注意此时gzip已经被自动解压缩了,因为上面的AutomaticDecompression = DecompressionMethods.GZip)
    		Console.WriteLine(await response.Content.ReadAsStringAsync());
    	}
    
    }
    /// <summary>
    /// HttpClient实现Get请求(异步)
    /// </summary>
    static async void dooGet()
    {
    	string url = "http://localhost:52824/api/register?id=1";
    	//创建HttpClient(注意传入HttpClientHandler)
    	var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip };
    
    	using (var http = new HttpClient(handler))
    	{
    		//await异步等待回应
    		var response = await http.GetAsync(url);
    		//确保HTTP成功状态值
    		response.EnsureSuccessStatusCode();
    
    		//await异步读取最后的JSON(注意此时gzip已经被自动解压缩了,因为上面的AutomaticDecompression = DecompressionMethods.GZip)
    		Console.WriteLine(await response.Content.ReadAsStringAsync());
    	}
    }
    /// <summary>
    /// HttpClient实现Put请求(异步)
    /// </summary>
    static async void dooPut()
    {
    	var userId = 1;
    	string url = "http://localhost:52824/api/register?userid=" + userId;
    
    	//设置HttpClientHandler的AutomaticDecompression
    	var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip };
    	//创建HttpClient(注意传入HttpClientHandler)
    	using (var http = new HttpClient(handler))
    	{
    		//使用FormUrlEncodedContent做HttpContent
    		var content = new FormUrlEncodedContent(new Dictionary<string, string>()       
    		{
    		   {"Name","修改zzl"},
    		   {"Info", "Put修改动作"}//键名必须为空
    		});
    
    		//await异步等待回应
    
    		var response = await http.PutAsync(url, content);
    		//确保HTTP成功状态值
    		response.EnsureSuccessStatusCode();
    		//await异步读取最后的JSON(注意此时gzip已经被自动解压缩了,因为上面的AutomaticDecompression = DecompressionMethods.GZip)
    		Console.WriteLine(await response.Content.ReadAsStringAsync());
    	}
    }

  • 相关阅读:
    BootStrap Table前台和后台分页对JSON格式的要求
    神奇的外部嵌套(使用ROW_NUMBER()查询带条件的时候提示列名无效)
    要想获取select的值,使用ng-modle,否则无法获取select 的值
    Angular使用操作事件指令ng-click传多个参数示例
    Jenins 邮件通知
    Jenkins 流水线(Pipeline)
    Jenkins Master-Slave 架构
    Jenins 参数化构建
    Jenkins 用户权限管理
    Jenkins 安装
  • 原文地址:https://www.cnblogs.com/smartsmile/p/6234104.html
Copyright © 2011-2022 走看看