我嘗試從Azure中, 一個新的移動業務和數據化JSON正確曝光。Windows Phone 7的訪問Azure的移動服務(遠程服務器返回錯誤:未找到)
https://lifehope.azure-mobile.net/tables/USERPF
USERPF是一個示例表。
爲了簡化問題,我只是修改權限爲「大家」。
問題是下面列出的代碼不起作用。 錯誤消息是:遠程服務器返回錯誤:未找到 時,我打插入按鈕,插入USERPF的新紀錄......
private void butInsert_Click(object sender, RoutedEventArgs e)
{
USERPF item = new USERPF();
item.Column1 = 789;
item.Column2 = 789;
WebClient wc = new WebClient();
wc.Headers["Content-Type"] = "application/x-www-form-urlencoded";
//wc.Headers["X-ZUMO-APPLICATION"] = "";
wc.UploadStringCompleted += (ss, arg) =>
{
if (arg.Error == null)
{
MessageBox.Show("OK");
}
else
{
MessageBox.Show(arg.Error.Message);
}
};
wc.UploadStringAsync(
new Uri("https://lifehope.azure-mobile.net/tables/USERPF/"),
"POST", JsonHelper.ObjectToJson(item, typeof(USERPF)));
}
//USERPF.cs
public class USERPF
{
[System.Runtime.Serialization.IgnoreDataMember()]
public int id { get; set; }
[System.Runtime.Serialization.DataMember()]
public int Column1 { get; set; }
[System.Runtime.Serialization.DataMember()]
public int Column2 { get; set; }
}
//JsonHelper.cs
public static string ObjectToJson(object obj, Type type)
{
try
{
//Create a stream to serialize the object to.
MemoryStream ms = new MemoryStream();
// Serializer the User object to the stream.
DataContractJsonSerializer ser = new DataContractJsonSerializer(type);
ser.WriteObject(ms, obj);
byte[] json = ms.ToArray();
ms.Close();
return Encoding.UTF8.GetString(json, 0, json.Length);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return string.Empty;
}
}
非常感謝! – user1371541