实例介绍
JustEat.HttpClientInterception是一个针对.NET Standard 2.0及更高版本和.NET Framework 4.7.2的库,用于拦截服务器端的HTTP依赖。它基于DelegatingHandler的实现,可以直接作为HttpMessageHandler的实现使用,或者提供给HttpClient的实例。这使得在不需要托管HTTP服务器的情况下使用它变得轻量级。
安装:通过.NET SDK从NuGet安装JustEat.HttpClientInterception。
// 示例:拦截HTTP GET请求并返回自定义响应
var options = new HttpClientInterceptorOptions();
var builder = new HttpRequestInterceptionBuilder();
builder
.Requests()
.ForGet()
.ForHttps()
.ForHost("public.je-apis.com")
.ForPath("terms")
.Responds()
.WithJsonContent(new { Id = 1, Link = "https://www.just-eat.co.uk/privacy-policy" })
.RegisterWith(options);
using var client = options.CreateHttpClient();
// 执行
string json = await client.GetStringAsync("https://public.je-apis.com/terms");
此外,还可以通过"HTTP bundle"文件配置要拦截的HTTP请求及其相应的响应。这为在.NET应用程序中进行测试提供了强大的灵活性和控制能力。
【实例截图】
【核心代码】
文件清单
└── httpclient-interception-57f983d7bf51154d3deda83cc85cf782d7be58e2
├── build.ps1
├── CODE_OF_CONDUCT.md
├── Directory.Build.props
├── Directory.Build.targets
├── Directory.Packages.props
├── global.json
├── HttpClientInterception.ruleset
├── HttpClientInterception.sln
├── justeat-oss.snk
├── LICENSE
├── mdsnippets.json
├── NuGet.config
├── package-icon.png
├── README.md
├── samples
│ ├── README.md
│ ├── SampleApp
│ │ ├── appsettings.Development.json
│ │ ├── appsettings.json
│ │ ├── Extensions
│ │ │ └── HttpClientExtensions.cs
│ │ ├── Handlers
│ │ │ ├── AddRequestIdHandler.cs
│ │ │ └── TimingHandler.cs
│ │ ├── Program.cs
│ │ ├── Properties
│ │ │ └── launchSettings.json
│ │ ├── SampleApp.csproj
│ │ ├── Services
│ │ │ ├── IGitHub.cs
│ │ │ └── Repository.cs
│ │ └── wwwroot
│ │ └── favicon.ico
│ └── SampleApp.Tests
│ ├── HttpClientInterceptionFilter.cs
│ ├── HttpServerCollection.cs
│ ├── HttpServerFixture.cs
│ ├── ReposTests.cs
│ ├── SampleApp.Tests.csproj
│ ├── testsettings.json
│ └── xunit.runner.json
├── SECURITY.md
├── src
│ └── HttpClientInterception
│ ├── BundleExtensions.cs
│ ├── Bundles
│ │ ├── Bundle.cs
│ │ ├── BundleFactory.cs
│ │ ├── BundleItemConverter.cs
│ │ ├── BundleItem.cs
│ │ ├── BundleJsonSerializerContext.cs
│ │ └── http-request-bundle-schema.json
│ ├── DelegateHelpers.cs
│ ├── HttpClientInterceptorOptions.cs
│ ├── HttpClientInterceptorOptionsExtensions.cs
│ ├── HttpInterceptionResponse.cs
│ ├── HttpRequestInterceptionBuilder.cs
│ ├── HttpRequestInterceptionBuilderExtensions.cs
│ ├── HttpRequestNotInterceptedException.cs
│ ├── InterceptingHttpMessageHandler.cs
│ ├── JustEat.HttpClientInterception.csproj
│ ├── Matching
│ │ ├── DelegatingMatcher.cs
│ │ ├── RegistrationMatcher.cs
│ │ └── RequestMatcher.cs
│ ├── PublicAPI
│ │ ├── net472
│ │ │ ├── PublicAPI.Shipped.txt
│ │ │ └── PublicAPI.Unshipped.txt
│ │ ├── net6.0
│ │ │ ├── PublicAPI.Shipped.txt
│ │ │ └── PublicAPI.Unshipped.txt
│ │ ├── net7.0
│ │ │ ├── PublicAPI.Shipped.txt
│ │ │ └── PublicAPI.Unshipped.txt
│ │ └── netstandard2.0
│ │ ├── PublicAPI.Shipped.txt
│ │ └── PublicAPI.Unshipped.txt
│ ├── StringSyntaxAttribute.cs
│ ├── SystemTextJsonExtensions.cs
│ └── WarningMessages.cs
├── stylecop.json
└── tests
├── HttpClientInterception.Benchmarks
│ ├── CustomBenchmarkConfig.cs
│ ├── GitHubJsonSerializerContext.cs
│ ├── GitHubOrganization.cs
│ ├── IGitHub.cs
│ ├── InterceptionBenchmarks.cs
│ ├── JustEat.HttpClientInterception.Benchmarks.csproj
│ ├── organization.json
│ └── Program.cs
└── HttpClientInterception.Tests
├── BenchmarkTests.cs
├── Bundles
│ ├── BundleExtensionsTests.cs
│ ├── content-as-html-base64.json
│ ├── content-as-html-string.json
│ ├── content-as-json-array.json
│ ├── content-as-json-object.json
│ ├── content-as-null-json.json
│ ├── http-methods-versions.json
│ ├── http-request-bundle.json
│ ├── http-status-codes.json
│ ├── ignoring-path.json
│ ├── ignoring-query.json
│ ├── invalid-bundle-version.json
│ ├── invalid-content-format.json
│ ├── invalid-status-code.json
│ ├── invalid-uri.json
│ ├── invalid-version.json
│ ├── no-content.json
│ ├── no-uri.json
│ ├── null-items.json
│ ├── sample-bundle.json
│ ├── skipped-item-bundle.json
│ ├── templated-bundle-base64.json
│ ├── templated-bundle-json.json
│ ├── templated-bundle-json-no-parameters.json
│ ├── templated-bundle-null-headers.json
│ └── templated-bundle-string.json
├── example-bundle.json
├── Examples.cs
├── GitHub
│ ├── IGitHub.cs
│ └── Organization.cs
├── HttpAssert.cs
├── HttpClientInterceptorOptionsExtensionsTests.cs
├── HttpClientInterceptorOptionsTests.cs
├── HttpRequestInterceptionBuilderTests.cs
├── InterceptingHttpMessageHandlerTests.cs
├── JustEat.HttpClientInterception.Tests.csproj
└── xunit.runner.json
23 directories, 111 files
标签:
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论