Web API: Client: HttpClient Message Handlers
原文地址: http://www.asp.net/web-api/overview/web-api-clients/httpclient-message-handlers
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks; namespace Test.WebAPI.Client
{
public class CustomClientMessageHandler : DelegatingHandler
{
private int count = ;
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
count++;
Console.WriteLine(count); var response = await base.SendAsync(request, cancellationToken); if (!response.IsSuccessStatusCode)
{
Console.WriteLine("{0}\t{1}\t{2}", request.RequestUri, (int)response.StatusCode, response.Headers.Date);
} return response;
}
}
}
CustomClientMessageHandler.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Test.EntityFramework.Models; namespace Test.WebAPI.Client
{
class Program
{
static void Main(string[] args)
{ // Custom client message handler
TestCustomHanlder().Wait(); Console.WriteLine("Exist..."); Console.ReadLine();
} public static async Task TestCustomHanlder()
{
using (HttpClient client = HttpClientFactory.Create(new CustomClientMessageHandler()))
{
client.BaseAddress = new Uri("http://localhost:55165/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage response = await client.GetAsync("api/test"); // Blocking call // Get
if (response.IsSuccessStatusCode)
{
// Parse the response body. Blocking!
var products = await response.Content.ReadAsAsync<IEnumerable<Test.EntityFramework.Models.Test>>();
foreach (var p in products)
{
Console.WriteLine("{0}\t{1};", p.ID, p.Title);
}
}
else
{
Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
}
}
} }
}
Program.cs
Web API: Client: HttpClient Message Handlers的更多相关文章
- Web APi之HttpClient注意事项以及建议
Web APi之HttpClient注意事项以及建议 前言 之前对于用SelfHost来手动实现Web API的宿主模式,似乎不是太深入,所以本篇文章我们一起来讨论关于利用HttpClient来访问W ...
- NET Web API和Web API Client Gen使Angular 2应用程序
使用ASP.NET Web API和Web API Client Gen使Angular 2应用程序的开发更加高效 本文介绍“ 为ASP.NET Web API生成TypeScript客户端API ” ...
- [WEB API] CLIENT 指定请求及回应格式(XML/JSON)
[Web API] Client 指定请求及响应格式(xml/json) Web API 支持的格式请参考 http://www.asp.net/web-api/overview/formats-an ...
- Web APi之HttpClient注意事项以及建议(四)
前言 之前对于用SelfHost来手动实现Web API的宿主模式,似乎不是太深入,所以本篇文章我们一起来讨论关于利用HttpClient来访问Web API上的资源来进行探讨以及注意相关事项,希望此 ...
- WebApi2官网学习记录--HttpClient Message Handlers
在客户端,HttpClient使用message handle处理request.默认的handler是HttpClientHandler,用来发送请求和获取response从服务端.可以在clien ...
- Web API: Client: Call a Web API from a .net client
原文地址: http://www.asp.net/web-api/overview/web-api-clients/calling-a-web-api-from-a-net-client 翻译地址:h ...
- 用Web API Client 调用 Web API
安装Web API客户端库 右键单击项目,选择管理 NuGet程序包,选择联机选项,选择全部,在搜索框中输入“Microsoft.AspNet.WebApi.Client”, 搜索结果就是要安装的类库 ...
- 使用ASP.NET Web API和Web API Client Gen使Angular 2应用程序的开发更加高效
本文介绍“ 为ASP.NET Web API生成TypeScript客户端API ”,重点介绍Angular 2+代码示例和各自的SDLC.如果您正在开发.NET Core Web API后端,则可能 ...
- .net core web api 与httpclient发送和接收文件及数据
客户端 HttpClient var url = $"https://localhost:44323/api/values/posttest?resource_source=yangwwme ...
随机推荐
- Promise 记录
- [BUAA_SE_2017]提问回顾
提问回顾 学期初疑问回答 学期初疑问博客 教材中说,PM在衡量需求时需要方方面面的能力与研究.可是,当下许多互联网IT公司只承担外包业务,即客户给什么需求就实现什么需求,甚至可能不要求其它先进的功能. ...
- 微信小程序组件 分页菜单点击请求
//JS data: { navNum:0, navList: [ { id: 1, name: '已预约' }, { id: 2, name: '已消费' }, { id: 3, name: '已取 ...
- java异常处理的throw和throws的区别
1. 区别 throws是用来声明一个方法可能抛出的所有异常信息,throws是将异常声明但是不处理,而是将异常往上传,谁调用我就交给谁处理.而throw则是指抛出的一个具体的异常类型. 2.分别介绍 ...
- ACM数论之旅8---组合数(组合大法好(,,• ₃ •,,) )
组合数并不陌生(´・ω・`) 我们都学过组合数 会求组合数吗 一般我们用杨辉三角性质 杨辉三角上的每一个数字都等于它的左上方和右上方的和(除了边界) 第n行,第m个就是,就是C(n, m) (从0开始 ...
- Qt——线程与定时器
一.定时器QTimer类 The QTimer class provides repetitive and single-shot timers. The QTimer class provides ...
- 题解 P3870 【[TJOI2009]开关】
这个题我愣是交了好几遍没有过...... 后来@_皎月半洒花dalao告诉我说要^儿子节点的tag,然后就明白了...... 行吧,先上题面: 题目描述 现有N(2 ≤ N ≤ 100000)盏灯排成 ...
- Netty基础系列(3) --彻底理解NIO
前言 上一节中我们提到了同步异步与阻塞非阻塞的区别,知道了同步并不等于阻塞.而本节的主角NIO是一种同步非阻塞的I/O模型,并且是I/O多路复用模型.NIO在java中被称为 New I/O.它并不能 ...
- Linux下的bash对拍
下面是Linux下的bash对拍程序: #!/bin/bash t=0 //数据组数 while true; do let "t=$t + 1" echox printf $t / ...
- AFO NOI2018退役——菜鸡一直是菜鸡
游记DAY -INF连续几天的模拟让我确信我就是菜鸡.以及相信yxd,sjq,cyl神犇一定能够稳了. DAY 0报道,天很热热热热热热热热热. DAY 1开幕式,杜子德很热热热热热热热热热. DAY ...