我想在azure上更新blob存储中的json文件,而 WebClient.uploadData(url,data)
则给出了错误:
远程服务器返回错误:(405(资源不支持指定的http动词..
这样的代码:
[Route("PostJsonData")]
[HttpPost]
public void PostJSONData(string value)
{
try
{
string url = @"https://apkupdates.blob.core.windows.net/polaadapk/keywords.json";
byte[] json1 = new WebClient().DownloadData(url);
string result = System.Text.Encoding.UTF8.GetString(json1);
byte[] array = System.Text.Encoding.ASCII.GetBytes(value);
WebClient myWebClient = new WebClient();
Stream postStream = myWebClient.OpenWrite(url, "POST");
postStream.Write(array, 0, array.Length);
myWebClient.UploadData(url, array);
postStream.Close();
}
catch (Exception ex)
{
throw ex;
}
}
正如Gaurav已经提到的,您正在使用错误的动词。但是,我强烈建议您使用现有的 SDK 用于.net:
azure存储API for .net
SDK还实现了指数重试策略,以方便瞬态错误(503-服务不可用(,否则您必须实现自己。
上传的正确HTTP动词是blob是PUT
而不是POST
。因为您使用的是POST
而不是PUT
,您会遇到此错误。
请更改以下代码行:
Stream postStream = myWebClient.OpenWrite(url, "POST");
to
Stream postStream = myWebClient.OpenWrite(url, "PUT");
,您不应该获得此MethodNotAllowed (405)
错误。请注意,您可能会遇到403错误,因为该请求未经身份验证。我建议在继续之前阅读Storage Service REST API
文档。