Click here to Skip to main content
16,015,531 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there!
I've been musing over a Little Problem here,
i have a specific curl command that Needs to be sent using C#, and i Need to read the Response from cUrl.

I'd like to share the command i Need to "translate" and my solution for it (which doesn't work) and i'm hoping that maybe somebody can help me solve the Problem, i'd be thankful for any tipps or alternatives.

First of all, the cUrl command
curl \
-v\
--cacert some_file_i_dont_get\
--cert some_certificate\
--key some_key\
-XPOST\
-H "Content-Type: multipart/form-data"\
-F "file=filepath"\
-F "filename=somefilename"\
SOME_URL

there are a few things i don't get, like the "F" command, i'm not sure but i believe it defines Parameter names and values?

anyhow, i'm having Problems translating this command and especially getting how to add this data to a HttpRequest in C#

I'm gonna post my code into the "What have you tried" section to Show you my best guess

What I have tried:

C#
   public void MakeHttpsPostRequest()
        {
          
                 //Creating the X.509 certificate. ##########

            // use the TLS protocol 
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;

            // create HTTP web request with proper content type
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("someurl:443");
            request.ContentType = "application/xml;charset=UTF8";

            // grab the PFX as a X.509 certificate from disk
            string certFileName = Path.Combine("PATH TO FILE", "filename.file");

            // load the X.509 certificate and add to the web request
            X509Certificate2 cert = 
new X509Certificate2(certFileName, "(top-secret password)");

                //Set the Timeout.
                request.Timeout = 500;
                //Add certificate to request.
                request.ClientCertificates.Add(cert);
                 request.PreAuthenticate = true; //WUT?
                //UserAgent.
                 request.UserAgent = "My User Agent ?";
                //HTTP verb.
                request.Method = "POST";

            request.ContentType = "multipart/form-data";

              StringBuilder postData = new StringBuilder();
            postData.AppendUrlEncoded("cacert", "file");
            postData.AppendUrlEncoded("cert", "file");
            postData.AppendUrlEncoded("key", "file");
            postData.AppendUrlEncoded("filename", "file.file");
            postData.AppendUrlEncoded("file", "@filefile.file");

            ASCIIEncoding ascii = new ASCIIEncoding();
            byte[] postBytes = ascii.GetBytes(postData.ToString());
            
            request.ContentLength = postBytes.Length;
            Stream postStream = request.GetRequestStream();

            postStream.Write(postBytes,0,postBytes.Length);
           

            postStream.Flush();
            postStream.Close();
                //From HERE: Get HttpWebResponse.
            
}

and the AppendUrlEncoded Extension

C#
public static void AppendUrlEncoded(this StringBuilder sb, string name, string value)
       {
           if (sb.Length != 0)
               sb.Append("&");
           sb.Append(HttpUtility.UrlEncode(name));
           sb.Append("=");
           sb.Append(HttpUtility.UrlEncode(value));
       }
Posted
Updated 13-Feb-17 2:46am
v2

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