【发布时间】:2016-05-04 13:27:17
【问题描述】:
范围:
我正在尝试与外部集中式 Logging service 提供程序集成,使用 HTTPS 请求将日志发布到它。
我们在Mono 之上运行C#,使用Ubuntu 14.04 LTS 作为操作系统。
我们已经使用 mono 多年了,所以我们对它的行为和潜在的缺陷/问题非常熟悉。
以前的设置
当你在谷歌上搜索这个问题时,你会发现基本上有两种解决方案,对于这种情况,它们都没有对我有用。这是我到目前为止所做的事情
Basic Mono-Complete Setup + ca-certificates-mono(这可能会解决 HTTPS 相关问题)。
除此之外,我知道 Mono 默认不信任任何证书,它拥有自己的证书链,我们必须将它们导入其中。为此,我运行了mozroots --import --sync --ask-remove,它打印出“已下载并安装了 140 个证书”。
此外,我们使用这个讨厌的单行代码覆盖了 CertificateValidationCallback:
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => { return true; };
以上都没有解决我们的问题。
错误、错误和更多错误:
Note that all of the codes below do work on Windows.
到目前为止,我们已经尝试过:
- 使用本机 .NET WebClient 异步调用 (
PostAsync)。
因此,我们得到了诸如Cant find file system.net.http.dll 之类的错误,一旦我们将它从我们的Windows 系统复制到它,我们就会得到另一个错误Task Exception(不记得那里的确切消息)。
显然,在 Xamarin 程序上使用此客户端往往可以解决人们遇到的问题,但我们仍然会遇到上面列出的相同错误,使用来自 .NET 的标准 HttpClient 类
- 编写我们自己的 WebRequests Wrapper
这是我们得到的最接近实际解决方案的解决方案,在 Mono 上运行时会导致 Error Writing Headers。
小代码示例:
using (WebRequests webClient = new WebRequests ())
{
// Client Configuration
webClient.BufferSize = 32 * 1024;
webClient.Accept = "application/json";
webClient.ContentType = "application/json; charset=" + Encoding.UTF8.HeaderName;
webClient.Timeout = 60000;
webClient.ReadWriteTimeout = 60000;
webClient.Encoding = Encoding.UTF8.WebName;
// Dummy Logz Payload - One Json Per Line
string LogzPayload = "{id:'1', value='1'}\n{id:'2', value='2'}";
// Request to Logz
webClient.Post ("https://listener-4.logz.io:8071/?token=OUR_TOKEN&type=json", LogzPayload);
}
更新 1:
刚刚尝试运行以下命令并立即出现异常:
certmgr --ssl https://listener-4.logz.io:8071/?token=OUR_TOKEN&type=json --machine
例外:
Unhandled Exception:
System.IO.IOException: The authentication or decryption has failed. ---> System.IO.IOException: The authentication or decryption has failed. ---> Mono.Security.Protocol.Tls.TlsException: The authentication or decryption has failed.
at Mono.Security.Protocol.Tls.RecordProtocol.EndReceiveRecord (IAsyncResult asyncResult) <0x4192e470 + 0x00132> in <filename unknown>:0
at Mono.Security.Protocol.Tls.SslClientStream.SafeEndReceiveRecord (IAsyncResult ar, Boolean ignoreEmpty) <0x4192e3a0 + 0x00031> in <filename unknown>:0
at Mono.Security.Protocol.Tls.SslClientStream.NegotiateAsyncWorker (IAsyncResult result) <0x4192abb0 + 0x00225> in <filename unknown>:0
--- End of inner exception stack trace ---
【问题讨论】:
-
你用的是什么版本的 Mono?