How to use HTTP GET request in C# with SSL? (protocol violation)

后端 未结 3 1831
走了就别回头了
走了就别回头了 2020-12-13 22:55

I am currently trying to get a response from a server that is using SSL in C#. I have the code to do this in Java, but it looks like they do not translate 1:1.

I

3条回答
  •  一个人的身影
    2020-12-13 23:39

    HTTP conversations over SSL use a properly issued certificate for validation.

    You could use the RemoteCertificateValidationCallback delegate for validating the SSL certificate as follows:

    public static void ConnectSSL()
    {
    
        WebRequest request = WebRequest.Create("https://" + sslServerHost + ":" + sslServerPort);
        request.Proxy = null;
        request.Credentials = CredentialCache.DefaultCredentials;
    
       //allows for validation of SSL certificates 
    
        ServicePointManager.ServerCertificateValidationCallback += new  System.Net.Security.RemoteCertificateValidationCallback(ValidateServerCertificate);
    
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream dataStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(dataStream);
        string responseFromServer = reader.ReadToEnd();
    
    }
    
    //for testing purpose only, accept any dodgy certificate... 
    public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
    {
              return true; 
    }
    

提交回复
热议问题