2014-02-18 48 views
3

我試圖在C#中使用sonicAPI file/upload API等效於System.Net.Http.MultipartFormDataContent的「curl -F」參數?

我試圖將curl示例轉換爲C#,並且使用HttpClientMultipartFormDataContent返回錯誤400 /錯誤請求

內容的響應:在文檔中示出的捲曲的命令行的

<?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 https://api.sonicapi.com/file/upload?access_id=$ACCESS_ID [email protected]

代碼到目前爲止我製作:

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

我試圖從刪除但它沒有幫助。

注:上傳發生(即它不會立即返回)

你知道什麼是使用.NET的Web類當捲曲-F參數的相同呢?

編輯:使用繁瑣的請求的捲曲-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 

輸出的

輸出:

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 

(截斷)

+1

如果你(和/或向我們展示)的捲曲產生的HTTP請求查找(使用招,例如),並將它與您的代碼生成的HTTP請求時,它可能會提供一些見解。 – bzlm

+0

我已經添加了兩個輸出,不幸的是我無法獲得在Fiddly中運行的cmd的捲曲輸出,沒有任何東西被攔截。 – Aybe

+1

你必須[手動告訴curl使用代理(即「Fiddly」)](http://curl.haxx.se/mail/archive-2007-03/0032.html)。另外,第二個HTTP請求是什麼?它只是說你的問題中的「請求」,它看起來與你的其他HTTP請求(即「curl -v」)完全不同。另外,你的'curl -v'實際上是'curl -v -F ...'? – bzlm

回答

7

由於@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; 
} 
+0

不要忘記將此標記爲明天接受的答案。 – bzlm

+0

我還需要等一天才能標記出來。 – Aybe

+0

當你在等待的時候,你可以製作一個Sonic C#幫助程序庫,這樣其他人就可以輕鬆地使用C#來使用Sonic API,而不需要像以前那樣小心操作。您已經完成了一個輔助方法。 :) – bzlm