2012-11-12 62 views
1

我試圖插入文件在谷歌驅動器使用webRequest(因爲我正在實施一個可恢復的異步上傳),但我不知道如何把數據放在請求「身體」 。插入文件到谷歌驅動器與Web請求

從現在開始,我有:

public static HttpWebRequest CreateUploadRequest(Google.Apis.Drive.v2.DriveService driveService, string uri, Stream contentStream, string title, string mimeType, string description = null) 
    { 
     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); 

     request.Method = "PUT"; 

     Dictionary<string, object> requestBody = new Dictionary<string, object>(); 
     requestBody["title"] = title; 
     requestBody["mimeType"] = mimeType; 

     if (!string.IsNullOrWhiteSpace(description)) 
     { 
      requestBody["description"] = description; 
     } 

     driveService.Authenticator.ApplyAuthenticationToRequest(request); 


     Stream requestStream = request.GetRequestStream(); 

     //How to do that??? 

     requestStream.Close(); 

     return request; 
    } 

我設置了HttpWebRequest的頭,身體如何的數據應該被置於? 什麼是要插入的文件的byte []數據的屬性名稱?

任何例子,將不勝感激。

回答

3

this頁SDK文檔說:

請求的身體被格式化爲多部分/相關內容類型RFC2387和正好包含兩個部分。部件由邊界字符串標識,最後的邊界字符串後跟兩個連字符。

如果你從未使用過它,RFC2387是MIME的定義。在MIME二進制數據通常BASE64編碼,就像這個例子從RFC:

Content-Type: Application/octet-stream 
Content-Description: The fixed length records 
Content-Transfer-Encoding: base64 
Content-ID: <[email protected]> 

T2xkIE1hY0RvbmFsZCBoYWQgYSBmYXJtCkUgSS 
BFIEkgTwpBbmQgb24gaGlzIGZhcm0gaGUgaGFk 
IHNvbWUgZHVja3MKRSBJIEUgSSBPCldpdGggYS 
BxdWFjayBxdWFjayBoZXJlLAphIHF1YWNrIHF1 
YWNrIHRoZXJlLApldmVyeSB3aGVyZSBhIHF1YW 
NrIHF1YWNrCkUgSSBFIEkgTwo= 

看一看這個問題的二進制數據上傳到Web請求的例子:Upload files with HTTPWebrequest (multipart/form-data)

相關問題