none
HttpWebRequest returns error in c#, works in simple php example RRS feed

  • Question

  • I have working php example, which does its job, but can not make the same in .NET - returning:

    System.Net.WebException: The request was aborted: Could not create SSL/TLS secure channel. at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context) at System.Net.HttpWebRequest.GetRequestStream() at

    PHP Code:

    $curl = curl_init();
    $post_fields = "datta_toBeSent";
    $submit_url = "https://ecommerce.........";
    Curl_setopt($curl, CURLOPT_SSLVERSION, 1); //0 
    curl_setopt($curl, CURLOPT_POSTFIELDS, $post_fields);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, '0');
    curl_setopt($curl, CURLOPT_SSLCERT,         getcwd().'/sert.pem');
    curl_setopt($curl, CURLOPT_SSLKEYPASSWD,   'XXXXXXXX');
    curl_setopt($curl, CURLOPT_URL, $submit_url);
    $result = curl_exec($curl);
    $info = curl_getinfo($curl);

    .NET Code

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
    string addr1 = "https://ecommerce......";
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(addr1);
    
    req.Method = "POST"; // Post method
    System.Net.ServicePointManager.ServerCertificateValidationCallback =
        delegate
        {
            return true; // **** Always accept
        };
    
    req.KeepAlive = false;
    req.ProtocolVersion = HttpVersion.Version10;
    
    //Certificate with private key
    string certName = "ssssss.xxx.ge_523300ff954_merchant_wp";
    string certpwd = "xxxxxxxx";
    X509Certificate2 cert = new X509Certificate2(@"c:\xxx\ssss\" + certName + ".pem", certpwd);
    
    req.ServerCertificateValidationCallback =
        delegate
        {
            return true; // **** Always accept
        };
    
    req.ClientCertificates.Add(cert);
    
    //req.PreAuthenticate = false;
    
    string langStr = "";
    String XML = "datatobesent";
    
    byte[] buffer = Encoding.ASCII.GetBytes(XML);
    req.ContentLength = buffer.Length;
    
    // Wrap the request stream with a text-based writer
    
    Stream writer = req.GetRequestStream();
    writer.Write(buffer, 0, buffer.Length);
    writer.Close();
    
    WebResponse rsp = req.GetResponse();
    String post_response = "";
    using (StreamReader responseStream = new StreamReader(rsp.GetResponseStream()))
    {
    }

    I have tried many variations of ignoring cert errors, or playing with tls ssl ver sions etc, but none seems to be working I have tried many variations of ignoring cert errors, or playing with tls ssl ver sions etc, but none seems to be working.

    Also , trying code from similar posts does not help either.

    Thank you.



    • Edited by AleksD223 Friday, March 15, 2019 3:03 AM
    Saturday, March 2, 2019 2:17 AM

All replies

  • Hi AleksD223,

    Thank you for posting here.

    For your question, try the code below.

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

    If this code does not work, please provide more details. What is the Windows system version and what is the .net framework version?

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 4, 2019 2:31 AM
    Moderator
  • Wendy, thank you for getting back to me.

    I have tried your suggestion, but getting same error: "System.Net.WebException: The request was aborted: Could not create SSL/TLS secure channel. at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context) at System.Net.HttpWebRequest.GetRequestStream()"

    .NET Version is 4.6.2

    Windows is Server 2012 R2

    Application is hosted as standard IIS App...

    any advice how to proceed? 

    Friday, March 8, 2019 7:40 AM
  • Hi AleksD223,

    You could try to do some changes in Registry. But based on my search, I do not find for 2012 R2.

    https://support.microsoft.com/en-sg/help/3140245/update-to-enable-tls-1-1-and-tls-1-2-as-default-secure-protocols-in-wi

    https://tecadmin.net/enable-tls-on-windows-server-and-iis/

    Best Regards,

    Wendy

    Note: This response contains a reference to a third-party World Wide Web site. Microsoft is providing this information as a convenience to you. 
    Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there.
    There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet. 


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, March 12, 2019 6:31 AM
    Moderator
  • I don't know why, but turns out sometimes specifying correct ContentLength might cause this problem.
    Just removing req.ContentLength = buffer.Length; 
    solved the issue.
    Friday, March 15, 2019 2:48 AM