2013-05-06 71 views
0

我想创建一个文件夹,上传文件,然后使用REST API 我的代码是这样的如何创建文件夹,并使用REST API和OAuth2.0的

public string CreateFolder(string FolderName) 
    { 
     int WorkSpaceId = 330201; 
     int id = 168079105; 
     var queryString = HttpContext.Current.Session["tokenSession"]; 
     var request = WebRequest.Create(RequestProfileUrl + FolderName); 
     request.AuthenticationLevel = System.Net.Security.AuthenticationLevel.None; 
     request.Headers.Add("Authorization", "Bearer " + AccessToken); 
     request.ContentType = "multipart/form-data"; 
     request.Method = "POST"; 
     var response = request.GetResponse(); 
     HttpContext.Current.Response.Redirect("" + request.RequestUri); 
     using (var responseStream = response.GetResponseStream()) 
     { 
      var reader = new StreamReader(responseStream); 
      var responseText = reader.ReadToEnd(); 
      reader.Close(); 
      return responseText; 
     } 
    } 

我必须做这样的

上传文件
POST https://apis.live.net/v5.0/me/skydrive 
Authorization: Bearer ACCESS_TOKEN 
Content-Type: multipart/form-data 

{ 
    "name": "My example folder" 
}` 

我添加了请求头和内容类型,我不知道如何将name参数添加到我的请求中。

+0

为什么'Java'标记? – 2013-05-06 13:37:20

+0

我有oauth2.0和其他api的问题,所以我加了java也 – user2322397 2013-05-06 13:41:19

回答

0

要编写POST请求的正文,您需要首先获取请求流,然后写入请求流。请参阅下面的示例代码。请注意,我将Content-Type从“multipart/form-data”更改为“application/json”,因为这是您的数据似乎是。

 // String with the body content 
     string postBody = "{\"name\":\"myfoldername\"}"; 
     ASCIIEncoding encoding = new ASCIIEncoding(); 
     byte[] byte1 = encoding.GetBytes (postBody); 

     // Set Content type to application/json 
     myHttpWebRequest.ContentType = "application/json"; 

     // Set content length of the string being posted. 
     myHttpWebRequest.ContentLength = byte1.Length; 

     // Get the request stream and write body bytes to it 
     Stream newStream = myHttpWebRequest.GetRequestStream(); 
     newStream.Write (byte1, 0, byte1.Length); 
0

我相信应该放在你的请求中。您可能需要调用writeMessageBody并根据需要编写正文。

+0

request.writemessagebodu不存在 – user2322397 2013-05-06 13:42:49