c# - System.Net.Http.MultipartFormDataContent 的 "curl -F"参数是否等效?

标签 c# curl multipartform-data dotnet-httpclient

我正在尝试使用 sonicAPI file/upload API在 C# 中。

我尝试使用 HttpClientMultipartFormDataContent 将 curl 示例转换为 C# 时返回错误 400/Bad Request

响应内容:

<?xml version="1.0" encoding="UTF-8"?>
<response>
   <status code="400" />
   <errors>
      <error message="File upload failed: file is missing." parameter="file" error_code="10" />
   </errors>
</response>

文档中显示的 curl 命令行示例:

curl https://api.sonicapi.com/file/upload?access_id=$ACCESS_ID -Ffile=@Vocals.mp3

到目前为止我编写的代码:

public async Task<HttpResponseMessage> Post(string id, string fileName)
{
    string url = string.Format("http://api.sonicapi.com/file/upload?access_id={0}", id);
    var stream = new FileStream(fileName, FileMode.Open);

    var client = new HttpClient { Timeout = TimeSpan.FromMinutes(10) };
    var content = new MultipartFormDataContent();
    content.Add(new StreamContent(stream), "file");

    HttpResponseMessage message = await client.PostAsync(url, content);
    string s = await message.Content.ReadAsStringAsync();

    return message;
}

我尝试从 content.Add(new StreamContent(stream), "file"); 中删除 "file" 但没有帮助。

注意:上传发生(即它不会立即返回)

您知道在使用 .NET 网络类时 curl -F 参数的等价物是什么吗?

编辑:

curl -v 的输出

* Hostname was NOT found in DNS cache
*   Trying 87.106.252.119...
* Connected to api.sonicapi.com (87.106.252.119) port 80 (#0)
> POST /file/upload?access_id=xxxxxxxxxxxx HTTP/1.1
> User-Agent: curl/7.35.0
> Host: api.sonicapi.com
> Accept: */*
> Content-Length: 882266
> Expect: 100-continue
> Content-Type: multipart/form-data; boundary=------------------------b3c6dc0fc9
34fc71
>
< HTTP/1.1 100 Continue
< HTTP/1.1 201 Created
* Server nginx/0.7.67 is not blacklisted
< Server: nginx/0.7.67
< Date: Tue, 18 Feb 2014 21:14:09 GMT
< Content-Type: application/xml
< Connection: keep-alive
< X-Powered-By: Express
< X-Sonicapi-Request-Id: 6422cd9a-6069-4c2f-a3c5-0865c8ada6d5
< Access-Control-Allow-Origin: *
< location: /file/download?file_id=dae4e051-fe11-4058-a009-855dbb74de50
< X-Sonicapi-File-Id: dae4e051-fe11-4058-a009-855dbb74de50
< Content-Length: 249
<
<?xml version="1.0" encoding="utf-8"?><response><status code="201"/><file file_i
d="dae4e051-fe11-4058-a009-855dbb74de50" status="ready" href="/file/download?fil
e_id=dae4e051-fe11-4058-a009-855dbb74de50" remaining_lifetime_seconds="3599"/></
response>* Connection #0 to host api.sonicapi.com left intact

使用 Fiddly 的请求输出:

POST http://api.sonicapi.com/file/upload?access_id=xxxxxxxx
HTTP/1.1
Content-Type: multipart/form-data; boundary="bd6fba7f-c173-4470-9c44-c9cc91f618a9"
Host: api.sonicapi.com
Content-Length: 882175
Expect: 100-continue
Connection: Keep-Alive

--bd6fba7f-c173-4470-9c44-c9cc91f618a9
Content-Disposition: form-data; name=file

RIFFvu
�WAVEfmt

(截断)

最佳答案

感谢@bzlm,使用Fiddler我设法追踪丢失的东西:

  • 内容配置
  • 内容类型

并且这些需要在 streamContent 而不是 content 上设置。

public async Task<HttpResponseMessage> Post(string id, string fileName)
{
    string url = string.Format("http://api.sonicapi.com/file/upload?access_id={0}", id);
    var stream = new FileStream(fileName, FileMode.Open);
    string name = Path.GetFileName(fileName);

    var client = new HttpClient { Timeout = TimeSpan.FromMinutes(10) };

    var streamContent = new StreamContent(stream);
    streamContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data");
    streamContent.Headers.ContentDisposition.Name = "\"file\"";
    streamContent.Headers.ContentDisposition.FileName = "\"" + name + "\"";
    streamContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");

    var content = new MultipartFormDataContent { streamContent };
    HttpResponseMessage message = await client.PostAsync(url, content);
    string s = await message.Content.ReadAsStringAsync();
    return message;
}

关于c# - System.Net.Http.MultipartFormDataContent 的 "curl -F"参数是否等效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21865205/

相关文章:

linux - Enctype ="multipart/form-data"在 linux 环境下不工作?

c# - 特殊类型查询的数据结构

c# - List<Interface> 的 object[] 返回 null

php - 如何在 PHP 中使用 cURL 找到我将被重定向的位置?

ssl - curl:(60)SSL证书问题:在代理后面上传时

ios - AFNetworking V 2 是否支持非流式多部分后台上传任务?

java - 如何在 Spring-boot 2.x 中一起发送 MultiPart 和 RequestBody

c# - 如何验证 ASP .NET MVC 中的名称以便允许使用重音符号(é、á、...)?

c# - 防止在 MySQL 数据库中插入相同的值

PHP cURL 请求primary_ip 与cURL url 不同