0

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.

Lightzor
  • 1
  • 2
  • Maybe this will help you: https://stackoverflow.com/questions/5152723/curl-with-user-authentication-in-c-sharp – Auditive Oct 27 '21 at 15:48
  • @Fildor unfortunately, I can't use wireshark. I have to test on a server to acceed the IP and I don't any dev tools on it. – Lightzor Oct 27 '21 at 16:00
  • @Auditive it's not using httpclient :( – Lightzor Oct 27 '21 at 16:01
  • 1
    You need to understand what that `curl` command does first. There are several problems and contradictions. `Content-Disposition` is a *response* header. The code posts a JSON body when the `curl` command performs a FORM POST, hence the different media types. `curl` uses digest authentication, it doesn't send a username/password combination. – Panagiotis Kanavos Oct 27 '21 at 16:01

1 Answers1

0

curl -Fis for multipart -F, --form <name=content> Specify HTTP multipart POST data Try the following:

 var content = new MultipartFormDataContent();
 var fs = File.Open(".\cert.pem", FileMode.Open);
 var fc = new StreamContent(fs);
 fc.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
 content.Add(fc, "cert", "cert.pem");      

For information only curl headers received with a file cert.pem whose content is "cert".

POST
Partial body: --------------------------87b709a918c0166a
Content-Disposition: form-data; name="cert"; filename="cert.pem"
Content-Type: application/octet-stream

"cert"

--------------------------87b709a918c0166a--

Body: --------------------------87b709a918c0166a
Content-Disposition: form-data; name="cert"; filename="cert.pem"
Content-Type: application/octet-stream

"cert"

--------------------------87b709a918c0166a--

The equivalent received with proposed .NET code:

POST
Partial body: --7ee9988d-bfd9-46d6-b0c9-74af30d7a6a2
Content-Disposition: form-data; name=cert; filename=cert.pem; filename*=utf-8''cert.pem
Content-Type: application/octet-stream

"cert"

--7ee9988d-bfd9-46d6-b0c9-74af30d7a6a2--

Body: --7ee9988d-bfd9-46d6-b0c9-74af30d7a6a2
Content-Disposition: form-data; name=cert; filename=cert.pem; filename*=utf-8''cert.pem
Content-Type: application/octet-stream

"cert"

--7ee9988d-bfd9-46d6-b0c9-74af30d7a6a2--

This is not strictly the same for Content-Disposition: header value but its very close.

Hazrelle
  • 758
  • 5
  • 9
  • I already tried and I always receive a : "400 Bad request". – Lightzor Oct 27 '21 at 16:31
  • I've tested using a simple web node server with your `curl` command. Response from the server include a `Transfer-Encoding: chunked` Then `curl` send the a multipart as suggested. But the content is sent as `Content-Type: application/octet-stream`. So you should probably change the code to use a `StreamContent`. I'm going to update my answer. Results between curl and my tests are better. – Hazrelle Oct 27 '21 at 16:59
  • Hello, I adapted the code as suggested but still the same error. 400 Bad request. – Lightzor Oct 27 '21 at 17:49
  • At this stage you should probably perform a network trace on your computer to compare `curl` and `.net`. This may help a lot. – Hazrelle Oct 28 '21 at 08:11