Sending a request with HttpWebRequest over SSL

SSL Certification creation
-------------------------------------------------

Step1 : Download OpenSSL zip https://www.openssl.org/

Step2 : Create a workfolder
Go to work folder and follow the commands

Step3 : set RANDFILE=.rnd

Step4 : set OPENSSL_CONF=D:\SSL\openssl-64\openssl-64\openssl.cnf

Step5 : D:\SSL\openssl-64\openssl-64\bin\openssl.exe

Step6 : genrsa -out ConnexionMediaCA.key 4096

Step7 : req -new -x509 -days 1826 -key ConnexionMediaCA.key -out ConnexionMediaCA.crt

Step8 : genrsa -out Test.key 4096

Step9 : req -new -key Test.key -out Test.csr

Step10 : x509 -req -days 730 -in Test.csr -CA Test.crt -CAkey

Step11 : Test.key -set_serial 01 -out Test.crt

Step12 : pkcs12 -export -out Test.p12 -inkey Test.key -in

Step13 : Test.crt -chain -CAfile ConnexionMediaCA.crt

SSL creation Completed :)

If you want to convert certificate pfx format

Step14 : pkcs12 -export -out ConnexionMediaCA.pfx -inkey ConnexionMediaCA.key -in ConnexionMediaCA.crt

pkcs12 -export -out webservice.pfx -in vha_cert.pem -inkey vha_key_nopw.pem

pkcs12 -export -out Test.pfx -inkey Test.key -in

Test.crt


I have the following snippet in classic ASP, to send a command and retrieve the response over SSL:

string certFileName = Path.Combine(@"D:\SSL\Cert\""mycert.cer");
               //You must change the path to point to your .cer file location. 
               X509Certificate Cert = X509Certificate.CreateFromCertFile(certFileName);
               // Handle any certificate errors on the certificate from the server.
               ServicePointManager.CertificatePolicy = new CertPolicy();
               // You must change the URL to point to your Web server.
               HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("https://---/signin");
               Request.ClientCertificates.Add(Cert);
               Request.ContentType = "application/json; charset=utf-8";
               Request.Method = "POST";
               Request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)";
 
               using (var streamWriter = new StreamWriter(Request.GetRequestStream()))
               {
                   string json = "{\"username\":\"test\"," +
                                 "\"password\":\"password"}";
 
                   streamWriter.Write(json);
                   streamWriter.Flush();
                   streamWriter.Close();
               }
 
               var httpResponse = (HttpWebResponse)Request.GetResponse();
               using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
               {
                   var result = streamReader.ReadToEnd();
               }
 
 
               HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();
               // Print the repsonse headers.
               Console.WriteLine("{0}", Response.Headers);
               Console.WriteLine();
               // Get the certificate data.
               StreamReader sr = new StreamReader(Response.GetResponseStream(), Encoding.Default);
               int count;
               char[] ReadBuf = new char[1024];
               do
               {
                   count = sr.Read(ReadBuf, 0, 1024);
                   if (0 != count)
                   {
                       Console.WriteLine(new string(ReadBuf));
                   }
 
               } while (count > 0);
 
           }
           catch (Exception e)
           {
               Console.WriteLine(e.Message);
           } 


Search This Blog

Arsip Blog

Powered by Blogger.

Recent

Comment

Author Info

Like This Theme

Popular Posts

Video Of Day

jishnukanat@gmail.com

Sponsor

Most Popular