gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 00:24:46 28 4
gpt4 key购买 nike

我正在尝试使用 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com