2014-03-27 55 views
0

當我試圖上傳文件Box Storage使用框,但在響應時間,我收到此錯誤無法使用此動詞類型發送內容主體。而得到錯誤的反應時,試圖上傳文件箱收納

public static void UploadFileRequest(string FolderID, string accesstoken) 
     { 

      string boundary = string.Format("----------------------------{0}", DateTime.Now.Ticks.ToString("x")); 
      string filename="C:\\Users\\Administrator\\Desktop\\Text.txt"; 
      HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create("https://upload.box.com/api/2.0/files/content"); 
      ASCIIEncoding encoding = new ASCIIEncoding(); 

      string hh = "\"[email protected]\"" + filename + "\" "+";"+""; 
      hh += "parent_id=\"" + FolderID + "\""; 
      string kj = string.Format(("[email protected]" + filename)); 

      byte[] data = encoding.GetBytes(hh); 

      httpWReq.Headers.Add("Authorization", "Bearer " + accesstoken); 
      httpWReq.ContentType = "application/json"; 
      httpWReq.ContentLength = data.Length; 

      using (Stream stream = httpWReq.GetRequestStream()) 
      { 
       stream.Write(data, 0, data.Length); 
      } 

      HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse(); 

      string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd(); 
     } 

回答

0

提供的API不知道盒子API,我會假設上傳應該是一個POST操作,所以你需要指定您的要求正確的HTTP方法,在發送前:

httpWReq.Method = "POST"; 

Method屬性默認爲"GET"和GET操作確實不是n ormally有身體..

+0

弗雷德裏克你是對的,我錯過了POST方法有..但對我來說還是沒有反應。我認爲體格格式存在問題。 – user3463612

+0

獲取錯誤的響應(400),某處做錯誤在正文 串bodymessage =的String.Format( 「文件名= @ {0}&PARENT_ID = {1}」,文件名,FolderID); byte [] data = encoding.GetBytes(bodymessage); //獲得在下面線路錯誤的StreamReader(response.GetResponseStream())ReadToEnd的(); } – user3463612

+0

你是否在調用'GetResponse()'之前將你的字節寫入請求流? –

0

這裏是解決辦法,因爲C#接受字節格式,然後再上傳任何做,我缺少的是..希望它有助於

私人無效UploadBoxFile(字符串文件名){ HttpWebRequest req = HttpWebRequest.Create(「https://upload.box.com/api/2.0/files/content」)作爲HttpWebRequest; req.Method =「POST」; req.Headers.Add(「Authorization」,「Bearer < Access Token>」); req.ContentType = 「多部分/格式數據;邊界= \」 d174f29b-6def-47分貝-8519-3da38b21b398 \ 「」;

 string Content = GetFormatedData(Filename); 

     req.ContentLength = Content.Length; 
     using (Stream Writer = req.GetRequestStream()) 
     { 
      Writer.Write(Encoding.UTF8.GetBytes(Content), 0, Content.Length); 
     } 

     req.GetResponse(); 
    } 

    private string GetFormatedData(string Filename) 
    { 
     StringBuilder build = new StringBuilder(); 
     string Id = "d174f29b-6def-47db-8519-3da38b21b398"; 

     build.AppendLine("--" + Id); 
     build.AppendLine("Content-Disposition: form-data; filename=\"hello1.txt\"; name=\"filename\""); 
     build.AppendLine("Content-Type: application/octet-stream"); 
     build.AppendLine(); 

     string FileContent = "This is a sample text"; 
     build.AppendLine(FileContent); 

     build.AppendLine("--" + Id); 
     build.AppendLine("Content-Disposition: form-data; name=\"folder_id\""); 
     build.AppendLine(); 

     build.AppendLine("0"); 
     build.AppendLine("--" + Id + "--"); 


     return build.ToString(); 
    } 

謝謝..

相關問題