我正在尝试通过 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 来做到这一点,但我遇到了类似的问题。当我运行下面的代码时,我收到异常“提供的 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);
我在这里做错了什么?