0

我想创建一个文件夹并使用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向我的请求添加参数。

4

1 回答 1

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);
于 2013-05-06T14:57:15.570 回答