[英]convert curl command into c# code using httpclient
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 :我正在尝试的 C# 代码:
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 : aldo 测试了以下 c# 代码:
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.
使用 curl 命令可以正常工作。
curl -F
is for multipart -F, --form <name=content> Specify HTTP multipart POST data
Try the following: curl -F
用于 multipart -F, --form <name=content> Specify HTTP multipart POST data
尝试以下操作:
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".有关信息,只有随文件 cert.pem 接收的
curl
标头,其内容为“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:收到的等效 .NET 代码:
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.这与
Content-Disposition:
标头值并不完全相同,但非常接近。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.