【问题标题】:How to create folder and upload file using Rest api and oauth2.0如何使用 Rest api 和 oauth2.0 创建文件夹和上传文件
【发布时间】:2013-04-30 06:42:48
【问题描述】:

我想创建一个文件夹并使用rest api上传文件 我的代码是这样的

  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参数添加到我的请求中。

【问题讨论】:

  • 为什么要标记Java
  • 我对 oauth2.0 和 rest api 有问题,所以我也添加了 java

标签: c# java rest oauth-2.0 onedrive


【解决方案1】:

要编写 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-06
    • 1970-01-01
    • 2013-03-30
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多