Click here to Skip to main content
15,559,287 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to send HTTPwebRequest with certificate file(.cer) in c#. I am sending with TLS 1.2 and the same is enabled in server. I am getting request aborted and cannot create SSL/TLS Channel exception while fetching the response. I have imported the certificate file in the server .
Updated code is mentioned below:

What I have tried:

My code is as below:
private void btnInvoke_Click(object sender, EventArgs e)
        {
                                      
          
            try
            {

                txtLog.Text += "btnInvoke_Click()-Process started" + Environment.NewLine;
                CertificateOverride oCertOverride = new CertificateOverride();
                ServicePointManager.ServerCertificateValidationCallback = oCertOverride.RemoteCertificateValidationCallback;

                
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
                | SecurityProtocolType.Tls11
                | SecurityProtocolType.Tls12
                | SecurityProtocolType.Ssl3;


                ServicePointManager.Expect100Continue = true;



                txtLog.Text += "btnInvoke_Click()-Creating HttpWebRequest Request headers started" + Environment.NewLine;

                
                
                X509Certificate Cert = X509Certificate.CreateFromCertFile(@"D:\abc.cer");
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(txtRequestURL.Text);
                request.ClientCertificates.Add(Cert);
               
                if (chkRequestHeader.Checked)
                {
                   
                    request.Headers.Add("X-API-KEY", txtXAPIKey.Text);
                }
               
                ;
                request.ContentType = @"application/json";
                request.Accept = @"application/json";
                request.Method = "POST";

                request.Proxy = new WebProxy();
                request.ProtocolVersion = HttpVersion.Version10;
               
                request.Timeout = 1000 * Convert.ToInt32(txtResponseTimeOut.Text);
                
                
                
                string oRequest = txtReq.Text;

                using (Stream stm = request.GetRequestStream())
                {
                    txtLog.Text += "btnInvoke_Click()-Getting Request Stream started" + Environment.NewLine;
                    using (StreamWriter stmw = new StreamWriter(stm))
                    {
                        stmw.Write(oRequest);
                        stmw.Close();

                    }

                    txtLog.Text += "btnInvoke_Click()-Getting Request Stream Completed" + Environment.NewLine;
                }


                try
                {
                   
                    HttpWebResponse httpWebRes = (HttpWebResponse)request.GetResponse();
                    
                   
                    
                    using (StreamReader responseReader = new StreamReader(httpWebRes.GetResponseStream()))
                    {

                        try
                        {
                            txtLog.Text += "btnInvoke_Click()-Started Response Fetching from Response Stream" + Environment.NewLine;
                            txtResponse.Text = responseReader.ReadToEnd();
                            txtHTTPStatusCode.Text = Convert.ToString(httpWebRes.StatusCode);
                            
                            txtLog.Text += "btnInvoke_Click()-Completed Response Fetching from Response Stream" + Environment.NewLine;
                        }
                        catch (Exception ex)
                        {

                            txtLog.Text += "btnInvoke_Click()-Fetching from Response Stream Failed with Exception: " + ex.Message
                                + Environment.NewLine + " with Inner Exception:" + ex.InnerException + Environment.NewLine + " at Stack Trace: " + ex.StackTrace + Environment.NewLine;
                        }


                        responseReader.Close();


                    }
                   
                    
                    txtLog.Text += "btnInvoke_Click()-Fetching Response completed" + Environment.NewLine;
                }
               
                catch (Exception ex)
                {

                    txtLog.Text += "btnInvoke_Click()-Fetching Response from Response Stream  Process Failed with Exception: " + ex.Message
                              + Environment.NewLine + " with Inner Exception:" + ex.InnerException + Environment.NewLine + " at Stack Trace: " + ex.StackTrace + Environment.NewLine;

                }
                txtLog.Text += "btnInvoke_Click()-Process Completed" + Environment.NewLine;
            }
            catch (Exception ex)
            {
                txtLog.Text += "btnInvoke_Click()-Process Failed with Exception: " + ex.Message
                              + Environment.NewLine + " with Inner Exception:" + ex.InnerException + Environment.NewLine + " at Stack Trace: " + ex.StackTrace + Environment.NewLine;


            }




        }
Posted
Comments
Richard Deeming 30-Nov-22 4:11am    
REPOST
This is now your third virtually-identical question. Rather than constantly reposting the same thing over and over again, stop and engage with the people who are trying to help you.
Why I am getting exception "request aborted. Could not create SSL/TLS secure channel." in ASP.NET with C#?[^]
How to fetch response via httpwebrequest with cer certificate in C#?[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900