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接口参数标准化 ...
随机推荐
- PhantomJS用法示例
收录待用,修改转载已取得腾讯云授权 前言 大家有没有发现之前我们写的爬虫都有一个共性,就是只能爬取单纯的html代码,如果页面是JS渲染的该怎么办呢?如果我们单纯去分析一个个后台的请求,手动去摸索JS ...
- jbpm4(参数设置)
1.processDefinition.getDescription() <process name="task_test_2" xmlns="http://jbp ...
- Joomla详细安装图文教程
Joomla 详细安装图文教程 第一步,配置网站信息 配置数据库:这里我选择MySQLi,可以根据自己的选择 安装-- 安装完成!
- mac os x 安装adb
http://stackoverflow.com/questions/31374085/installing-adb-on-mac-os-x Option 1 - Using Homebrew Thi ...
- 《深入PHP:面向对象、模式与实践》(一)
第1章 PHP:设计与管理 本章主要介绍了本书有哪些内容. 第2章 PHP与对象 本章总结了PHP面向对象特性的发展过程,逐步介绍对象的概念. PHP/FI:支持变量.关联数组和函数.没有对象. ...
- Android五大布局标签和属性总结
1.LinearLayout orention 水平和垂直 weight 切割闲置空间 水平布局 切割宽度 垂直布局 切割的高度 切割的时候 指定的高度或者宽度不能用fill_parent/ ...
- JSON基本概念及使用
JSON:JavaScript 对象表示法(JavaScript Object Notation). JSON 是存储和交换文本信息的语法.类似 XML. JSON 比 XML 更小.更快,更易解析. ...
- 8.1.2 绑定Activity和Service
8.1.2 绑定Activity和Service 2010-06-21 16:57 李宁 中国水利水电出版社 字号:T | T <Android/OPhone开发完全讲义>第8章Andro ...
- 对象关系映射 EmitMapper 及Tuple的使用
public TDestination Map<TSource, TDestination>(TSource tSource) { if (tSource == null) return ...
- spring定时任务(@Scheduled注解)cron表达式详解
cron表达式详解: 一个cron表达式有至少6个(也可能7个)有空格分隔的时间元素. 按顺序依次为 秒(~) 分钟(~) 小时(~) 天(~) 月(~) 星期(~ =SUN 或 SUN,MON,TU ...