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",""},
{"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 = ;
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());
} 转载:https://blog.csdn.net/smartsmile2012/article/details/51613596
同步请求:
/// <returns></returns>
public static string DoorAllList()
{
var result = "";
string url = "http://www.baidu.com/Api/GetDoorUserList";
//创建HttpClientHandler的AutomaticDecompression
var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip };
//创建一个httpClient;
using (var client = new HttpClient(handler))
{
//同步请求
var response = client.GetAsync(url);
//在这里会等待response返回。
var ret = response.Result;
//最终等待返回结果
result = ret.Content.ReadAsStringAsync().Result;
}
return result;
}
WebApi~通过HttpClient来调用Web Api接口的更多相关文章
- c# 【MVC】WebApi通过HttpClient来调用Web Api接口
/// <summary> /// HttpClient实现Post请求(异步) /// </summary> static async void dooPost() { st ...
- Http下的各种操作类.WebApi系列~通过HttpClient来调用Web Api接口
1.WebApi系列~通过HttpClient来调用Web Api接口 http://www.cnblogs.com/lori/p/4045413.html HttpClient使用详解(java版本 ...
- WebApi系列~通过HttpClient来调用Web Api接口
回到目录 HttpClient是一个被封装好的类,主要用于Http的通讯,它在.net,java,oc中都有被实现,当然,我只会.net,所以,只讲.net中的HttpClient去调用Web Api ...
- 通过HttpClient来调用Web Api接口
回到目录 HttpClient是一个被封装好的类,主要用于Http的通讯,它在.net,java,oc中都有被实现,当然,我只会.net,所以,只讲.net中的HttpClient去调用Web Api ...
- WebApi系列~通过HttpClient来调用Web Api接口~续~实体参数的传递
回到目录 上一讲中介绍了使用HttpClient如何去调用一个标准的Web Api接口,并且我们知道了Post,Put方法只能有一个FromBody参数,再有多个参数时,上讲提到,需要将它封装成一个对 ...
- WebApi系列~通过HttpClient来调用Web Api接口~续~实体参数的传递 【转】
原文:http://www.cnblogs.com/lori/p/4045633.html 下面定义一个复杂类型对象 public class User_Info { public int Id { ...
- 通过HttpClient来调用Web Api接口~续~实体参数的传递
并且我们知道了Post,Put方法只能有一个FromBody参数,再有多个参数时,上讲提到,需要将它封装成一个对象进行传递,而这讲主要围绕这个话题来说,接口层添加一个新类User_Info,用来进行数 ...
- 通过HttpClient来调用Web Api接口,实体参数的传递
下面定义一个复杂类型对象 public class User_Info { public int Id { get; set; } public string Name { get; set; } p ...
- 【WebApi】通过HttpClient调用Web Api接口
HttpClient是一个封装好的类,它在很多语言中都有被实现,现在HttpClient最新的版本是4.5. 它支持所有的http方法,自动转向,https协议,代理服务器. 一.Api接口参数标准化 ...
随机推荐
- [Python爬虫] 之二十一:Selenium +phantomjs 利用 pyquery抓取36氪网站数据
一.介绍 本例子用Selenium +phantomjs爬取36氪网站(http://36kr.com/search/articles/电视?page=1)的资讯信息,输入给定关键字抓取资讯信息. 给 ...
- Druid和Spark对比
Druid和Spark对比 不是Spark专家, 如果描绘有错误, 请通过邮件列表或者其他方式告知我们 Spark实现弹性的分布式数据集概念的计算集群系统, 可以看做商业分析平台. RDDs能复用持久 ...
- [Angular] Angular Custom Change Detection with ChangeDetectorRef
Each component has its own ChangeDetectorRef, and we can inject ChangeDetectorRef into constructor: ...
- 在redhat下使用x11vnc进行桌面共享
1.在redhat上安装x11vnc时.你须要注意下面几个方面: (1)下载x11vnc的源代码包: 网址例如以下所看到的: http://sourceforge.net/projects/libvn ...
- javaEE开发之文件下载
package com.example.web; import java.io.File; import java.io.FileInputStream; import java.io.IOExcep ...
- mybatis 自动更新表结构 ,兼容通用tkmapper
1.maven引入jar <dependency> <groupId>com.github.gonglb.tools</groupId> <artifactI ...
- 【DB2】SQL0501N FETCH 语句或 CLOSE 语句中指定的游标尚未打开,或者游标标量函数引用中的游标变量尚未打开。 SQLSTATE=24501
在DB2中建立存储过程时使用了隐式游标,在调用的时候报错如下: SQL0501N FETCH 语句或 CLOSE 语句中指定的游标尚未打开,或者游标标量函数引用中的游标变量尚未打开. SQLSTA ...
- activiti designer下载地址
http://www.activiti.org/designer/update/ http://www.activiti.org/designer/archived/ 这个地址貌似不能用了 ...
- Spring 常用注入注解(annotation)和其对应xml标签
使用注解需要修改bean.xml: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=& ...
- C# 调用adb command 读取手机型号和IMEI
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...