Friday, October 12, 2012

Create Https Request C#

I was writing a client application to communicate with our  server application built on asp.net.
client application worked perfactly with http port but when i tried to connect it to https port which was using a self signed certificate it raises an error
"The remote certificate is invalid according to the validation procedure" so for my debugging purpose i needed to bypass that certificate checking so i tried like that

provide a RemoteCertificateValidationCallback delegate



private static bool ValidateCertificate(object sender,X509Certificate cert,X509Chain chain,SslPolicyErrors polErrors)
        {
            if (Convert.ToBoolean("true"))
            {
                // allow any old dodgy certificate...
                return true;
            }
            else
            {
                return polErrors == SslPolicyErrors.None;
            }
        }

and attach it to ServerCertificateValidationCallback property of ServicePointManager.just before the line where Getting Response from server


ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(ValidateCertificate);
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myRequest.GetResponse();

Also included the following namespaces as well

 System.Security.Cryptography.X509Certificates;
 System.Net.Security;