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:
public void MakeHttpsPostRequest()
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("someurl:443");
request.ContentType = "application/xml;charset=UTF8";
string certFileName = Path.Combine("PATH TO FILE", "filename.file");
X509Certificate2 cert =
new X509Certificate2(certFileName, "(top-secret password)");
request.Timeout = 500;
request.ClientCertificates.Add(cert);
request.PreAuthenticate = true;
request.UserAgent = "My User Agent ?";
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();
}
and the AppendUrlEncoded Extension
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));
}