我试图在不使用TableContext和Table ServiceEntity类的情况下写入Azure表存储,我只想发送原始XML(我希望能够灵活地定义模式)。
当我这样做(并使用SDK中的方法签署请求)时,我得到了404.不知道为什么。
该表存在。我已经省略了我的凭据,但它们在原始代码中是正确的。
下面的代码段说明了问题。
非常欢迎任何建议!
using System.IO;
using System.Net;
using System.Text;
using Microsoft.WindowsAzure.StorageClient.Protocol;
public static class Program
{
public static void Main()
{
Credentials credentials = new Credentials("xxx", @"yyy");
var uri = string.Format(@"http://{0}.table.core.windows.net/{1}(PartitionKey='{2}',RowKey='{3}')", credentials.AccountName, "TableName", "1", "1");
var webRequest = (HttpWebRequest)WebRequest.Create(uri);
webRequest.Method = "POST";
webRequest.Headers.Add("DataServiceVersion", "2.0;NetFx");
webRequest.Headers.Add("MaxDataServiceVersion", "2.0;NetFx");
webRequest.ContentType = @"application/atom+xml";
WriteToRequestStream(uri, webRequest);
TableRequest.SignRequestForSharedKeyLite(webRequest, credentials);
var response = webRequest.GetResponse(); // 404 thrown here
}
private static void WriteToRequestStream(string uri, HttpWebRequest webRequest)
{
var sb = new StringBuilder();
sb.Append(@"<?xml version='1.0' encoding='utf-8' standalone='yes'?><entry xmlns:d='http://schemas.microsoft.com/ado/2007/08/dataservices' xmlns:m='http://schemas.microsoft.com/ado/2007/08/dataservices/metadata' xmlns='http://www.w3.org/2005/Atom'><title /><updated>2009-03-18T11:48:34.9840639-07:00</updated><author><name /></author><id>");
sb.Append(uri);
sb.Append(@"</id><content type='application/xml'><m:properties></m:properties></content></entry>");
string body = sb.ToString();
webRequest.ContentLength = body.Length;
UTF8Encoding encoding = new UTF8Encoding();
byte[] bytes = encoding.GetBytes(body);
using (Stream requestStream = webRequest.GetRequestStream())
{
requestStream.Write(bytes, 0, bytes.Length);
}
}
}
答案 0 :(得分:1)
创建实体时,您有三种选择:
http://myaccount.table.core.windows.net/mytable
的http://myaccount.table.core.windows.net/mytable(PartitionKey="myPartitionKey", RowKey="myRowKey1")
http://myaccount.table.core.windows.net/mytable(PartitionKey="myPartitionKey", RowKey="myRowKey1")
然而,后两者不受本地模拟器的支持。
此外,我查看了您的代码使用Fiddler进行的请求,并且缺少以下标题:
要正确包含标题,请尝试以下代码:
var webRequest = (HttpWebRequest)WebRequest.Create(uri);
webRequest.Method = "PUT";
webRequest.Headers.Add("DataServiceVersion", "2.0;NetFx");
webRequest.Headers.Add("MaxDataServiceVersion", "2.0;NetFx");
webRequest.Headers.Add("x-ms-version", "2011-08-18");
webRequest.ContentType = @"application/atom+xml";
TableRequest.SignRequestForSharedKeyLite(webRequest, credentials);
WriteToRequestStream(uri, webRequest);
var response = webRequest.GetResponse();
注意我是如何更改方法来执行插入或替换的。我还将调用反转为SignRequestForSharedKeyLite
并添加了x-ms-version
标头版本2011-08-18
,否则不支持插入或替换。
答案 1 :(得分:1)
请参阅https://msdn.microsoft.com/en-us/library/azure/dd179338.aspx上的表格存储的类型限制。例如,当发布具有最小数据时间值的属性时,将导致404
答案 2 :(得分:0)
我认为你错过了'x-ms-date'标题。我认为标题是必需的(参见http://msdn.microsoft.com/en-us/library/windowsazure/dd179433.aspx)。
有关使用REST API的完整示例,可能需要查看http://azurestoragesamples.codeplex.com/。大多数操作都包含在那里。