C# WebRequest 使用 .NET 成功但使用 Mono 失败
C# WebRequest succed with .NET but failed using Mono
我正在尝试访问托管在网络服务器上的 Rest API。
此服务器具有自签名证书。
因此出于开发目的,我覆盖了 ServicePointManager 的 ServerCertificateValidationCallback
我的程序是这样的:
class Program
{
static void Main(string[] args)
{
string BASE_URL = "api-endpoint-addr";
string param = "some-param";
GetRequest(BASE_URL + param);
Console.ReadLine();
}
/**
* Accept any certificate (for dev purpose)
*/
private static bool TrustCertificate(object sender, X509Certificate x509Certificate, X509Chain x509Chain, SslPolicyErrors sslPolicyErrors)
{
// all Certificates are accepted
Console.WriteLine("Accepting anyway...");
return true;
}
static void GetRequest(string uri)
{
ServicePointManager.ServerCertificateValidationCallback = TrustCertificate;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.BeginGetResponse(ResponseCallback, request);
}
static private void ResponseCallback(IAsyncResult result)
{
Console.WriteLine("Response CB");
HttpWebResponse response = (result.AsyncState as HttpWebRequest).EndGetResponse(result) as HttpWebResponse;
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
Console.WriteLine("responseFromServer=" + responseFromServer);
}
}
当它在 VisualStudio 中使用 .NET 构建此代码时一切正常,我可以连接到服务器并获取 JSON 但是当尝试使用 Mono 构建它时,从未调用 TrustCertificate 回调(我不't "Acceptiong anyway..." 在控制台中)并且程序停止并出现以下错误:
Unhandled Exception : System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive. ---> System.IO.IOException: unable to read data from the transport connection : an existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: an existing connection was forcibly closed by the remote host
这里有什么问题?我真的不明白为什么 Mono
失败了
正在添加
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
开头的GetRequest(uri)解决了问题
我正在尝试访问托管在网络服务器上的 Rest API。 此服务器具有自签名证书。 因此出于开发目的,我覆盖了 ServicePointManager 的 ServerCertificateValidationCallback
我的程序是这样的:
class Program
{
static void Main(string[] args)
{
string BASE_URL = "api-endpoint-addr";
string param = "some-param";
GetRequest(BASE_URL + param);
Console.ReadLine();
}
/**
* Accept any certificate (for dev purpose)
*/
private static bool TrustCertificate(object sender, X509Certificate x509Certificate, X509Chain x509Chain, SslPolicyErrors sslPolicyErrors)
{
// all Certificates are accepted
Console.WriteLine("Accepting anyway...");
return true;
}
static void GetRequest(string uri)
{
ServicePointManager.ServerCertificateValidationCallback = TrustCertificate;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.BeginGetResponse(ResponseCallback, request);
}
static private void ResponseCallback(IAsyncResult result)
{
Console.WriteLine("Response CB");
HttpWebResponse response = (result.AsyncState as HttpWebRequest).EndGetResponse(result) as HttpWebResponse;
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
Console.WriteLine("responseFromServer=" + responseFromServer);
}
}
当它在 VisualStudio 中使用 .NET 构建此代码时一切正常,我可以连接到服务器并获取 JSON 但是当尝试使用 Mono 构建它时,从未调用 TrustCertificate 回调(我不't "Acceptiong anyway..." 在控制台中)并且程序停止并出现以下错误:
Unhandled Exception : System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive. ---> System.IO.IOException: unable to read data from the transport connection : an existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: an existing connection was forcibly closed by the remote host
这里有什么问题?我真的不明白为什么 Mono
失败了正在添加
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
开头的GetRequest(uri)解决了问题