2012-12-04 75 views
3

我正在尝试使用rest api将文件上传到box.net。但我每次都收到404错误。 这里请求标题(来自fiddler)。我犯了什么错误?将文件上传到Box.net

POST https://api.box.com/2.0/files/content HTTP/1.1 
Authorization: BoxAuth api_key={key}&auth_token={tokem} 
Content-Type: multipart/form-data; boundary="13afaf22-f210-464b-bcc3-3cd3e4ed1617" 
Host: api.box.com 
Content-Length: 166 
Expect: 100-continue 

--13afaf22-f210-464b-bcc3-3cd3e4ed1617 
Content-Disposition: form-data; filename=test.zip; folder_id=0 
{empty line - I don't know why it here} 
{bytes starting here} 
--13afaf22-f210-464b-bcc3-3cd3e4ed1617-- 

注意我使用C#与它的HttpClient类和MultiPartFormDataContent作为内容源。

解决:

问题得到解决。请求头和身体应该是这样的:

POST https://api.box.com/2.0/files/content HTTP/1.1 
Authorization: BoxAuth api_key={key}&auth_token={token} 
Content-Type: multipart/form-data; boundary="d174f29b-6def-47db-8519-3da38b21b398" 
Host: api.box.com 
Content-Length: 314 
Expect: 100-continue 

--d174f29b-6def-47db-8519-3da38b21b398 
Content-Disposition: form-data; filename="hello.txt"; name="filename" 
Content-Type: application/octet-stream 

{Bytes} 
--d174f29b-6def-47db-8519-3da38b21b398 
Content-Disposition: form-data; name="folder_id" 

0 
--d174f29b-6def-47db-8519-3da38b21b398-- 

感谢

+1

你可以发布你的解决方案作为这个问题的答案吗? – seanrose

回答

5

好吧,这里是方法,它上传文件Box.com如果有人感兴趣

我使用的是从类。净4.5。

public async Task Upload(string authorization, string filename, string parentID) 
    { 
     HttpRequestMessage message = new HttpRequestMessage(); 
     message.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("BoxAuth", authorization); 


     MultipartFormDataContent content = new MultipartFormDataContent(); 
     StreamContent streamContent = null; 

     streamContent = new StreamContent(new FileStream(localURI, FileMode.Open)); 

     streamContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") 
     { 
      FileName = "\"" + filename + "\"", 
      Name = "\"filename\"" 
     }; 
     streamContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); 
     content.Add(streamContent); 

     ByteArrayContent byteContent = new ByteArrayContent(parentID.ToByteArray()); 
     byteContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") 
     { 
      Name = "\"folder_id\"" 
     }; 

     content.Add(byteContent); 
     message.Method = HttpMethod.Post; 
     message.Content = content; 

     message.RequestUri = new Uri("https://api.box.com/2.0/files/content"); 

     HttpResponseMessage response = null; 

     Task<HttpResponseMessage> t = httpClient.SendAsync(message, cancelationToken); 
     response = await t; 

     if (t.IsCompleted) 
     { 
      if (!response.IsSuccessStatusCode) 
      { 
       if (response.Content != null) 
        Logger.Error(await response.Content.ReadAsStringAsync(), "Box Upload"); 
       else 
        Logger.Error("Error", "Box Upload"); 
      } 
     } 
    } 
+1

谢谢你回来和回答你自己的问题,它确实帮助不太受欢迎的标签。 – Pomster

+0

当我尝试上传大文件(> 25MB)时,这对我来说是失败的,我在Windows Mobile 8开发中使用此代码,请告知我可能的原因。 –

+0

这可能是由于HttpClient的写入超时造成的。 – Vlad