等效"卷曲-F"参数System.Net.Http.MultipartFormDataContent? [英] Equivalent of "curl -F" parameter for System.Net.Http.MultipartFormDataContent?

查看:803
本文介绍了等效"卷曲-F"参数System.Net.Http.MultipartFormDataContent?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用 sonicAPI文件/上传API在C#

我试图用的HttpClient 卷曲例子,C#翻译和 MultipartFormDataContent 返回错误的 400 /错误请求

My attempt to translate the curl example to C# with HttpClient and MultipartFormDataContent returns the error 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命令行的例子:

Example of the curl command-line shown in the documentation :

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

代码中,我至今制作的:

Code I've crafted so far :

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(新StreamContent(流),程序文件); ,但它并没有帮助

请注意:上传的情况(即不立即返回)

Note : the upload happens (i.e. it does not return immediately)

你知道当使用的是卷曲-F参数相当于?.NET的Web类

Do you know what is the equivalent of the curl -F parameter when using .NET web classes ?

编辑:

卷曲-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

的输出使用繁琐的要求:

Output of the request using 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



(截断)

(truncated)

推荐答案

由于@bzlm,使用的提琴手我设法追踪什么丢失:

Thanks to @bzlm, using Fiddler I managed to track what was missing :


  • 内容处置

  • 内容类型

和这些有待在 streamContent 而不是内容

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;
}

这篇关于等效&QUOT;卷曲-F&QUOT;参数System.Net.Http.MultipartFormDataContent?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆