Search code examples
c#asp.netcurlhttpwebrequestx509certificate2

Convert Curl Command to C#


I am trying to convert a Curl Command to C#, This is how the command looks

curl -E <partner>.crt --key <partner>.key https://APIDomain/returns/?wsdl -v --no-alpn

This will return a wsdl file, Now how can I convert this code to c#? Note: API requires me to authenticate the call with a X509 certificate, That's why the .crt and .key files are used here. I have tried to achieve the same by below code, But it throws an error

fiddler.network.https> HTTPS handshake to APIDomain/ (for #1) failed. System.IO.IOException Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. < An existing connection was forcibly closed by the remote host On Fiddler

 string host = @"https://APIDomain/returns/?wsdl";
            string certName = @"C:\Cert\mydomain.pfx";
            string password = @"xxxxxxxxx";

            try
            {
                X509Certificate2 certificate = new X509Certificate2(certName, password);

                ServicePointManager.CheckCertificateRevocationList = false;
                ServicePointManager.ServerCertificateValidationCallback = (a, b, c, d) => true;
                ServicePointManager.Expect100Continue = true;
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(host);
                req.PreAuthenticate = true;
                req.AllowAutoRedirect = true;
                req.ClientCertificates.Add(certificate);
                req.Method = "POST";
                req.ContentType = "application/x-www-form-urlencoded";
                string postData = "login-form-type=cert";
                byte[] postBytes = Encoding.UTF8.GetBytes(postData);
                req.ContentLength = postBytes.Length;

                Stream postStream = req.GetRequestStream();
                postStream.Write(postBytes, 0, postBytes.Length);
                postStream.Flush();
                postStream.Close();
                WebResponse resp = req.GetResponse();

                Stream stream = resp.GetResponseStream();
                using (StreamReader reader = new StreamReader(stream))
                {
                    string line = reader.ReadLine();
                    while (line != null)
                    {
                        Console.WriteLine(line);
                        line = reader.ReadLine();
                    }
                }

                stream.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

Solution

  • Can you try the following code?

    var handler = new HttpClientHandler();
        handler.ClientCertificateOptions = ClientCertificateOption.Manual;
        handler.SslProtocols = SslProtocols.Tls12;
        handler.ClientCertificates.Add(new X509Certificate2("<partner>.crt", key));
        var client = new HttpClient(handler);
        var result = client.GetAsync("https://APIDomain/returns/?wsdl").GetAwaiter().GetResult();