2

I was trying to read the data returned from a url. Consider the following code:

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;
}

I was thrown an exception:

System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive. ---> System.ComponentModel.Win32Exception: The client and server cannot communicate, because t hey do not possess a common algorithm

on the following code:

WebResponse response = request.GetResponse();

I have already verified by accessing the _targetUrl directly via the browser in which the proxy was set the same as the proxy defined in the code and definitely able to see data returned on the browser.

I came with the coding above by taking bits and pieces from references I found searching around. Most suggestions I found seems to be almost identical to the one I'm using right now.

A bit of lost here. Can somebody point me the right direction?

Thank you for your kind attention guys.

Khairul
  • 191
  • 2
  • 14
  • What version of .Net Framework are you using? I suspect the issues is `ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12` and the remote server doesn't support v1.2. Try removing that line. – DavidG Jul 11 '16 at 09:14
  • Hi @DavidG, the code was run a machine installed with .NET 4.0. I've removed the line but was thrown a different exception **System.Net.WebException: The request was aborted: Could not create SSL/TLS secure channel.** on the same `WebResponse response = request.GetResponse();` line – Khairul Jul 11 '16 at 09:40

0 Answers0