原文地址: 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的更多相关文章

  1. Web APi之HttpClient注意事项以及建议

    Web APi之HttpClient注意事项以及建议 前言 之前对于用SelfHost来手动实现Web API的宿主模式,似乎不是太深入,所以本篇文章我们一起来讨论关于利用HttpClient来访问W ...

  2. 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 ” ...

  3. [WEB API] CLIENT 指定请求及回应格式(XML/JSON)

    [Web API] Client 指定请求及响应格式(xml/json) Web API 支持的格式请参考 http://www.asp.net/web-api/overview/formats-an ...

  4. Web APi之HttpClient注意事项以及建议(四)

    前言 之前对于用SelfHost来手动实现Web API的宿主模式,似乎不是太深入,所以本篇文章我们一起来讨论关于利用HttpClient来访问Web API上的资源来进行探讨以及注意相关事项,希望此 ...

  5. WebApi2官网学习记录--HttpClient Message Handlers

    在客户端,HttpClient使用message handle处理request.默认的handler是HttpClientHandler,用来发送请求和获取response从服务端.可以在clien ...

  6. 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 ...

  7. 用Web API Client 调用 Web API

    安装Web API客户端库 右键单击项目,选择管理 NuGet程序包,选择联机选项,选择全部,在搜索框中输入“Microsoft.AspNet.WebApi.Client”, 搜索结果就是要安装的类库 ...

  8. 使用ASP.NET Web API和Web API Client Gen使Angular 2应用程序的开发更加高效

    本文介绍“ 为ASP.NET Web API生成TypeScript客户端API ”,重点介绍Angular 2+代码示例和各自的SDLC.如果您正在开发.NET Core Web API后端,则可能 ...

  9. .net core web api 与httpclient发送和接收文件及数据

    客户端 HttpClient var url = $"https://localhost:44323/api/values/posttest?resource_source=yangwwme ...

随机推荐

  1. js 杂项(一)函数篇

    你还应该知道箭头函数( => )可以用来保留上下文.这个方法也可以:

  2. NET Core Mvc发布带视图文件的方法!

    添加节点:<MvcRazorCompileOnPublish>false</MvcRazorCompileOnPublish>

  3. vue 使用出现的问题(持续记录)

    今天写vue 的时候,发现有几个警告.原因是 我把组件起的名字写的和默认标签的名字一样了,导致系统不知道,怎么解析. 我写了一个Header 组件, 和h5里面的header重名, 解决方案1: he ...

  4. VMWare之——宿主机与虚拟机互相ping通,宿主机ping通另一台机器的虚拟机

    版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请注明出处:http://blog.csdn.NET/l1028386804/article/details/52267554 今天给大家带来 ...

  5. 使用docker-compose编排django、mysql实战

    背景: 本萌最近在部署自己开发的项目的时候发现同一套代码上传到服务器上后,部分功能莫名其妙的有点问题,服务器的各项配置都没有做过变动,所以想把项目转战到docker. 奈何刚接触docker,很多地方 ...

  6. [转帖]UsingDiskspdforSQLServer --- 改天做下练习 官方文档.

    Using DiskSpd in SQL Server environments Writer: Robert Beene Contributors: Jose Barreto, Ramu Konid ...

  7. [转帖]真TM长的:SQL Server 2008存储结构——GAM和SGAM、PFS结构、IAM结构、DCM&BCM

    谈到GAM和SGAM,我们不得不从数据库的页和区说起. https://blog.csdn.net/snowfoxmonitor/article/details/49991015 一个数据库由用户定义 ...

  8. vue & button & refs & click & bug

    vue & button & refs & click & bug $refs.btn.click() ??? vue & refs $refs.btn.$em ...

  9. php 关于文件的一些封装好的函数

    <?php //Bytes/Kb/MB/GB/TB/EB /** * 转换字节大小 * @param number $size * @return number */ function tran ...

  10. rabbitmq 集群安装

    1.安装模式分为三种:单主机模式.普通集群模式.镜像集群模式. 单主机模式:rabbitmq运行在一台主机上,生产环境不建议使用该模式,性能有限.如果该台主机down机,整个服务将不可用. 普通集群模 ...