我尝试通过REST API将文件上传到SkyDrive。我一直在使用以下代码,但我一直在使用"(415)不支持的媒体类型。"错误:
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();
我也尝试过使用RestSharp来做这件事,但我遇到了类似的问题。当我运行下面的代码时,我得到了异常&#34;提供的Content-Type标题&#39; multipart / form-data;边界\ u003d ----------------------------- 28947758029299&#39;不受支持&#34;。
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);
我在这里做错了什么?
答案 0 :(得分:5)
好的,我解决了。显然,我需要做的就是将ContentType留空并且有效。谢谢:))