简体   繁体   English

c#使用Skydrive REST API进行PUT请求时遇到问题

[英]c# Trouble with PUT request using Skydrive REST API

I'm trying to upload a file to SkyDrive via the REST API. 我正在尝试通过REST API将文件上传到SkyDrive。 I've been using the following code, but I keep getting a "(415) Unsupported Media Type." 我一直在使用以下代码,但是我一直收到“(415)不支持的媒体类型”。 error: 错误:

            var requestUriFile =
                             new StringBuilder("https://apis.live.net/v5.0/<folderid>/files/testfile.txt");
        requestUriFile.AppendFormat("?access_token={0}", accessTokenM);

        byte[] arr = System.IO.File.ReadAllBytes("C:\\temp\\testFile.txt");
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUriFile.ToString());
        request.Method = "PUT";
        request.ContentType = "text/plain";
        request.ContentLength = arr.Length;
        Stream dataStream = request.GetRequestStream();
        dataStream.Write(arr, 0, arr.Length);
        dataStream.Close();
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        string returnString = response.StatusCode.ToString();

I've also tried using RestSharp to do this, but I'm running into a similar issue. 我也尝试过使用RestSharp来执行此操作,但是我遇到了类似的问题。 When I run the below code, I get returned the exception "The provided Content-Type header 'multipart/form-data; boundary\=-----------------------------28947758029299' is not supported". 当我运行以下代码时,我返回了异常“提供的Content-Type标头'multipart / form-data; boundary \\ u003d --------------------- -------- 28947758029299'不支持”。

            byte[] arr = System.IO.File.ReadAllBytes("C:\\temp\\testFile.txt");

        var client = new RestClient("https://apis.live.net/v5.0/");
        var request = new RestRequest(Method.PUT);
        request.Resource = "<folderId>/files/testfile.txt?access_token=" + accessTokenM;
        request.AddHeader("content-type", "text/plain;");
        request.AddFile("filename", arr, "testfile.txt", "text/plain");



        var responseIn = client.Execute(request);

What am I doing wrong here? 我在这里做错了什么?

Ok, I solved it. 好的,我解决了。 Apparently all I need to do is leave the ContentType blank and it works. 显然,我需要做的就是将ContentType保留为空白,并且可以正常工作。 Thanks :) 谢谢 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM