c#程序错误“不支持所请求的安全协议”

问题描述 投票:0回答:1

我在下面的代码中使用过

try
{
    ServicePointManager.ServerCertificateValidationCal  lback = delegate { return true; };
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;

    WebRequest req = WebRequest.Create(site);
    req.ContentType = "application/x-www-form-urlencoded";
    req.Method = "POST";
    byte[] bytes = Encoding.UTF8.GetBytes("data=" + "null");
    req.ContentLength = bytes.Length;
    Stream os = req.GetRequestStream();
    os.Write(bytes, 0, bytes.Length);
    os.Close();
    WebResponse resp = req.GetResponse();
    StreamReader sr = new StreamReader(resp.GetResponseStream());

    MessageBox.Show(sr.ReadToEnd().Trim());
}
catch (Exception exc)
{
    MessageBox.Show(exc.Message.ToString());
}

程序在我的计算机上运行没有错误,但是在客户计算机上运行时显示以下错误“不支持所请求的安全协议”

继续,我将代码更改为以下

try
{
    WebRequest req = WebRequest.Create(site);
    req.ContentType = "application/x-www-form-urlencoded";
    req.Method = "POST";
    byte[] bytes = Encoding.UTF8.GetBytes("data=" + "null");
    req.ContentLength = bytes.Length;
    Stream os = req.GetRequestStream();
    os.Write(bytes, 0, bytes.Length);
    os.Close();
    WebResponse resp = req.GetResponse();
    StreamReader sr = new StreamReader(resp.GetResponseStream());


    MessageBox.Show(sr.ReadToEnd().Trim());
}
catch (Exception exc)
{
    MessageBox.Show(exc.Message.ToString());
}

但还是错误。

我的客户的计算机是donNet 4的win7。

c#
1个回答
0
投票

要摆脱“不支持所请求的安全协议”错误,您可以尝试使用SecurityProtocolType.Tls12,类似的东西

ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

或使用SecurityProtocolType.Default作为recommended

© www.soinside.com 2019 - 2024. All rights reserved.