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

时间:2019-12-25 15:36:16

标签: c#

我使用了以下代码

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。

1 个答案:

答案 0 :(得分:1)

您设置以下代码并进行测试

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls