如何解决C#HTTP请求中无法解决的远程名称;

时间:2018-08-03 06:01:07

标签: c# wpf https dns

我最近将后端服务器切换为https并投入生产,并将其部署在AWS EC2上,但是我的Windows客户端拒绝与服务器通信,并给我消息异常:

  

“无法解析远程名称”

这是示例代码

public bool AcceptAllCertifications(object sender, System.Security.Cryptography.X509Certificates.X509Certificatecertification, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
    return true;
}
var request = (HttpWebRequest) WebRequest.Create("https://api.mybackend.com/login");
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);

var data = Encoding.ASCII.GetBytes(User.Login());

request.Method = "POST";
request.ContentType = "application/json";
request.ContentLength = data.Length;

using (var stream = request.GetRequestStream())
{
    stream.Write(data, 0, data.Length);
}

try
{
    var response = (HttpWebResponse) request.GetResponse();

    var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
    if (responseString != null)
    {
        return GrailsController.Instance.User.Parse(responseString);
    }
}
catch (Exception e)
{
    return false;
}

例外情况发生在var response = (HttpWebResponse) request.GetResponse();上,出于对上帝的爱,我找不到解决该问题的方法。

0 个答案:

没有答案