简体   繁体   English

适用于Linux / Unix的替代HttpClientHandler

[英]alternative HttpClientHandler for Linux/Unix

I am looking for a portable drop in replacement for Mono HttpClient stack. 我正在寻找可移植的替代Mono HttpClient堆栈的替代品。 The portable Mono version [part of the regular Mono stack] has bugs. 便携式Mono版本(常规Mono堆栈的一部分)存在错误。 I have been trying to fix one specific bug affecting our project. 我一直在尝试解决一个影响我们项目的特定错误。 Due to highly asynchronous nature of the library necessary for async operations, lack of documentation and comments in the Mono code, it is taking a long time. 由于异步操作所需的库的高度异步性质,Mono代码中缺少文档和注释,因此需要很长时间。 I see that Microsoft does not bother implementing Http stack in C#, instead using reliable libraries - WinHttp and libcurl. 我看到Microsoft并不在意用C#实现Http堆栈,而是使用可靠的库-WinHttp和libcurl。 Unfortunately, porting libcurl to Mono is also a job. 不幸的是,将libcurl移植到Mono也是一项工作。 Legally, HttpClient allows replacing a handler, creating an alternative stack. 从法律上讲,HttpClient允许替换处理程序,从而创建备用堆栈。 There is a CFNetwork handler for iOS and ModernHttpClient for iOS/Android and other platforms. 有一个用于iOS的CFNetwork处理程序以及一个用于iOS / Android和其他平台的ModernHttpClient。 Yet, ModernHttpClient does not work, for me, as a drop-in replacement either. 但是,对我来说,ModernHttpClient不能作为嵌入式替代品使用。 Any suggestions? 有什么建议么? My code as below: 我的代码如下:

class Program
{
    static void Main(string[] args)
    {
        var threadCancelationToken = new CancellationTokenSource();
        var sl = new StatusLoop();
        sl.HttpClientTimeout = int.Parse(args[0]);
        sl.StatusFreq = int.Parse(args[1]);

       Task.Run(async () => await sl.RunStatusLoop(threadCancelationToken.Token), threadCancelationToken.Token);
       Console.ReadKey();
    }
}

public class StatusLoop
{
    public int HttpClientTimeout;
    public int StatusFreq;

    public async Task RunStatusLoop(CancellationToken cancellationToken)
    {
//       var handler = new HttpClientHandler
         var handler = new NativeMessageHandler
         {
            Credentials = new NetworkCredential("username", "password"),
            PreAuthenticate = true,
            UseProxy = false
         };

//       var httpClient = new HttpClient(new NativeMessageHandler())
         var httpClient = new HttpClient(handler)
         {
             Timeout = TimeSpan.FromSeconds(this.HttpClientTimeout)
         };

         httpClient.DefaultRequestHeaders.Add("Connection", "Keep-Alive");
         httpClient.DefaultRequestHeaders.ExpectContinue = false;

         ServicePointManager.ServerCertificateValidationCallback +=
    (sender, certificate, chain, sslPolicyErrors) => true;

        while (true)
        {
            try
            {
                var httpResponse = await httpClient.GetAsync("https://192.168.1.22/api/v1/system/status/", cancellationToken);
                if (httpResponse.IsSuccessStatusCode)
                    Console.Write(".");
            }
            catch (Exception ex)
            {
                Console.WriteLine ("HttpClientGetStatus - exception\n\r" + ex.ToString());
            }

            Thread.Sleep(this.StatusFreq * 1000);
        }
    }
}

If you have used an alternative HttpClientHandler on Linux/Mac, please share your experience. 如果您在Linux / Mac上使用了替代的HttpClientHandler,请分享您的经验。

Thank you 谢谢

有来源,一个在这里 ,但我不知道是否NuGet包包含此Unix程序代码还

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM