GithubHelp home page GithubHelp logo

thisboringworld / system.net.http.dotnetty Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 1.0 40 KB

基于DotNetty的HttpMessageHandler实现;用于HttpClient执行HTTP请求;

License: MIT License

PowerShell 0.36% C# 99.64%
dotnetty httpclienthandler http

system.net.http.dotnetty's Introduction

System.Net.Http.DotNetty

基于 DotNetty.Codecs.Http 实现的 HttpMessageHandler ; 目标框架为 netstandard2.0; 基本实现了代理请求、代理Basic认证、请求超时等基本功能并测试;内部实现了一个简单的连接池及其回收机制,并通过了简单的验证;直接替换 HttpClientHandler 就能使用;整体上测试了基本的使用情况,比较正常。对比原生实现好像还没发现大优势,这个要自行体验;

1. 如何使用

暂无Nuget,下载源码进行编译并引用

示例方式都应该以单例模式使用,请求是线程安全的

1.1 以HttpMessageHandler的方式使用

1.1.1 简单的使用方式:

var client = new HttpClient(new HttpDotNettyClientHandler());

1.1.2 如果需要自定义某些配置的话:

var option = new DotNettyClientOptions()
{
    //设置可选项
};
var client = new HttpClient(new HttpDotNettyClientHandler(option));

然后使用 HttpClient 像以前一样进行请求就行了;

1.2 以DotNetty的方式使用

直接使用 DotNettyHttpRequestExecutor 类的对象进行请求;

1.2.1 引用命名空间

注意有类型会和 System.NetSystem.Net.Http 下的类型冲突

using System.Net.Http.DotNetty;
using DotNetty.Codecs.Http;
using DotNetty.Common.Utilities;

1.2.2 创建执行器,请求,并获取结果

var option = new DotNettyClientOptions()
{
    //设置可选项
};
var executor = new DotNettyHttpRequestExecutor(option);

var uri = new Uri("http://www.baidu.com");

var request = new DefaultFullHttpRequest(HttpVersion.Http11, HttpMethod.Get, uri.PathAndQuery);

IFullHttpResponse response = null;
try
{
    response = await executor.ExecuteAsync(request, uri, proxyUri: null, CancellationToken.None).ConfigureAwait(false);

    //信息获取
    var html = response.Content.ReadString(response.Content.WriterIndex, Encoding.UTF8);
}
finally
{
    response.SafeRelease();
}

2. Link

system.net.http.dotnetty's People

Contributors

stratosblue avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

dwjujq

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.