使用HttpClient 调用Web Api
C#4.5 添加了异步调用Web Api 。
如果你的项目是4.5以上版本,可以直接参考官方文档。
http://www.asp.net/web-api/overview/web-api-clients/calling-a-web-api-from-a-net-client
现在项目使用的是C# 4.0 ,需要对HttpClient 进行修改。
当然了你要先添加两个引用 Microsoft.Net.Http和Microsoft.AspNet.WebApi.Client
下面是改造后的HttpClient
public class ResultMessage
{
public string Code { get; set; }
public string Message { get; set; }
public string Content { get; set; }
} public class HttpClientHelper
{
public static string RequestUrl = "http://localhost:1282/"; /// <summary>
/// HttpClient实现Get请求
/// </summary>
public static ResultMessage Get(string url)
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(RequestUrl);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); Task<HttpResponseMessage> response = client.GetAsync(url);
var resultMsg = new ResultMessage();
if (response.Result.IsSuccessStatusCode)
{
Task<string> content = response.Result.Content.ReadAsStringAsync();
string resultjson = content.Result;
resultMsg = JsonConvert.DeserializeObject<ResultMessage>(resultjson);
Console.WriteLine("{0}\t{1}\t{2}", resultMsg.Code, resultMsg.Message, resultMsg.Content);
}
else
{
resultMsg.Code = response.Result.StatusCode.ToString();
resultMsg.Message = response.Result.ReasonPhrase;
Console.WriteLine("{0} ({1})", (int) response.Result.StatusCode, response.Result.ReasonPhrase);
} return resultMsg;
}
} /// <summary>
/// HttpClient实现Post请求
/// 执行服务方法
/// </summary>
public static ResultMessage Post(string code, string name)
{
string url = RequestUrl + "api/Biz/Exec"; using (var client = new HttpClient())
{
client.BaseAddress = new Uri(RequestUrl);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var content = new FormUrlEncodedContent(new Dictionary<string, string>
{
{"code",code}
{"name",name},
});
var resultMsg = new ResultMessage();
Task<HttpResponseMessage> response = client.PostAsync(url, content);
//延时处理
if (response.Result.IsSuccessStatusCode)
{
Task<string> con = response.Result.Content.ReadAsStringAsync();
string resultjson = con.Result;
resultMsg = JsonConvert.DeserializeObject<ResultMessage>(resultjson);
}
else
{
resultMsg.Code = response.Result.StatusCode.ToString();
resultMsg.Message = response.Result.ReasonPhrase;
} return resultMsg;
}
}
}
使用HttpClient 调用Web Api的更多相关文章
- 【ASP.NET Web API2】利用HttpClient调用Web API(TODO)
参照: 在一个空ASP.NET Web项目上创建一个ASP.NET Web API 2.0应用 纯属记录一下遇到的问题: 我们利用HttpClient来调用自宿主方式寄宿的Web API.HttpCl ...
- 【WebApi】通过HttpClient调用Web Api接口
HttpClient是一个封装好的类,它在很多语言中都有被实现,现在HttpClient最新的版本是4.5. 它支持所有的http方法,自动转向,https协议,代理服务器. 一.Api接口参数标准化 ...
- 【ASP.NET Web API教程】3.2 通过.NET客户端调用Web API(C#)
原文:[ASP.NET Web API教程]3.2 通过.NET客户端调用Web API(C#) 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本博客文章,请先看前面的 ...
- 通过.NET客户端调用Web API(C#)
3.2 Calling a Web API From a .NET Client (C#) 3.2 通过.NET客户端调用Web API(C#) 本文引自:http://www.asp.net/web ...
- 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 ...
- Http下的各种操作类.WebApi系列~通过HttpClient来调用Web Api接口
1.WebApi系列~通过HttpClient来调用Web Api接口 http://www.cnblogs.com/lori/p/4045413.html HttpClient使用详解(java版本 ...
- 【ASP.NET Web API教程】3.3 通过WPF应用程序调用Web API(C#)
原文:[ASP.NET Web API教程]3.3 通过WPF应用程序调用Web API(C#) 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本博客文章,请先看前面的 ...
- Dynamics 365 CE的插件/自定义工作流活动中调用Web API示例代码
微软动态CRM专家罗勇 ,回复325或者20190428可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me! 现在Web API越来越流行,有时候为了程序更加健壮,需要在插件 ...
随机推荐
- xmlplus 组件设计系列之七 - 路由
在浏览器端,对路由的理解一般是根据不同的 URL 完成页面的切换.在服务器端,则是根据不同的 URL 请求回馈相关的页面.在本章,我们讲述的是根据接收到的不同命令,路由组件呈现出不同的页面,这算是广义 ...
- 个人VIM配置实例
用户 vimrc 文件: "$HOME/.vimrc" " vimrc by lewiyon@hotmail.com " last update 2013-10 ...
- selenium IDE的3种下载安装方式
第一种方式: 打开firefox浏览器-----点击右上角-----附加组件----插件----搜索框输入“selenium”-----搜索的结果中下拉到页面尾部,点击“查看全部的37项结果”---进 ...
- 【MySql】——MHA+GTID+failover+binlog-server+Atlas
一.环境准备 1.mysql-db01 #系统版本 [root@mysql-db01 ~]# cat /etc/redhat-release CentOS release 6.7 (Final) #内 ...
- CSS之定位布局(position,定位布局技巧)
css之定位 1.什么是定位:css中的position属性,position有四个值:absolute/relative/fixed/static(绝对/相对/固定/静态(默认))通过定位属性可以设 ...
- python-day2 字典
===========字典功能=============> dict.clear() -->清空字典 dict.keys() -->获取所有key dict.values() --& ...
- apache代理转发
打开apache安装目录的conf文件夹下的httpd.conf1.将以下两行前的注释字符 # 去掉:#LoadModule proxy_module modules/mod_proxy.so#Loa ...
- 学习笔记:javascript body常用事件
Window 事件属性 针对 window 对象触发的事件(应用到 <body> 标签): 属性 值 描述 onafterprint script 文档打印之后运行的脚本. onbefor ...
- Linux文件查看与查找命令
cat 查看一个文件 -E: 显示行结束符$ -n: 对显示出的每一行进行编号 -A:显示所有控制符 -b:非空行编号 -s:压缩连续的空行成一行 -T:显示制表符 常用:cat -An /etc/ ...
- Rabin-Karp字符串查找算法
1.简介 暴力字符串匹配(brute force string matching)是子串匹配算法中最基本的一种,它确实有自己的优点,比如它并不需要对文本(text)或模式串(pattern)进行预处理 ...