System.ComponentModel.Win32Exception:客户端和服务器无法通信,因为它们没有通用的算法

时间:2016-07-11 09:13:00

标签: c# httpwebrequest servicepointmanager

我试图读取从网址返回的数据。请考虑以下代码:

private static void Read()
{
    _targetUrl = "https://url";
    _proxyUrl = "http://differentUrl:8080";

    ServicePointManager.ServerCertificateValidationCallback += AcceptAllCertifications;
    ServicePointManager.Expect100Continue = true;
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; 

    WebProxy webProxy = new WebProxy
    {
        Address = _proxyUri,
        BypassProxyOnLocal = false,
        UseDefaultCredentials = false
    };

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_targetUrl);
    request.Proxy = webProxy;
    request.Method = "GET"; 

    WebResponse response = request.GetResponse();
    Stream stream = response.GetResponseStream();

    string ret = "";
    byte[] buffer = new byte[1048];
    int read;
    while (stream != null && (read = stream.Read(buffer, 0, buffer.Length)) > 0)
    {
        Console.Write(Encoding.ASCII.GetString(buffer, 0, read));
        ret += Encoding.ASCII.GetString(buffer, 0, read);
    }

    Console.WriteLine("READ: \n{0}", ret);
}

private static bool AcceptAllCertifications(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors policyErrors)
{
    return true;
}

我被抛出异常:

  

System.Net.WebException:底层连接已关闭:An   接收时发生意外错误。 --->   System.ComponentModel.Win32Exception:客户端和服务器不能   沟通,因为他们没有共同的算法

以下代码:

WebResponse response = request.GetResponse();

我已经通过浏览器直接访问_targetUrl进行了验证,其中代理的设置与代码中定义的代理相同,并且能够看到浏览器返回的数据。

我带来了上面的编码,我从搜索到的参考文献中搜索了一些碎片。我发现的大多数建议似乎与我现在使用的建议几乎相同。

这里有点丢失。有人能指出正确的方向吗?

感谢您的关注。

0 个答案:

没有答案