Command I need to convert :
curl.exe --digest -u login:pw -s -F "cert=@.\cert.pem" http://127.0.0.1/upload.htm
C# code I'm trying :
HttpClientHandler handler = new HttpClientHandler();
handler.Credentials = new NetworkCredential("login", "pw");
HttpClient client = new HttpClient(handler);
client.BaseAddress = new Uri("http://127.0.0.1");
...
var content = new StringContent(fileContents);
content.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "cert",
FileName = "cert.pem"
};
await client.PostAsync("/upload.htm", content);
Result :
<body><h1>HTTP/1.0 415 Unsupported Media Type</h1></body>
aldo tested the following c# code :
string executableLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
String path = Path.Combine(executableLocation, "cert.pem");
var fs = File.Open(path, FileMode.Open);
var multiPartContent = new MultipartFormDataContent();
var fc = new StreamContent(fs);
fc.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
multiPartContent.Add(fc, "certUsageUnspecified", "cert.pem");
var uploadCertificate = await client.PostAsync("/upload.htm", multiPartContent);
logger.Info(await uploadCertificate.Content.ReadAsStringAsync());
logger.Info("=== end upload certificate ===");
and result is the following :
<body><h1>HTTP/1.0 400 Bad Request</h1></body>
I don't know what I'm doing wrong there but I can't find the solution. It's working fine with the curl command.