Windows Phone 7 Access Azure Mobile Service (il server remoto ha restituito un errore: non trovato)
-
13-12-2019 - |
Domanda
Provo a creare un nuovo servizio mobile da Azure, E i dati sono stati correttamente esposti da JSON.
https://lifehope.azure-mobile.net/tables/USERPF .
Userpf è una tabella dei campioni.
Per semplificare la domanda, ho appena modificato il permesso a "tutti".
I problemi sono il codice elencato di seguito non funziona. Il messaggio di errore è: Il server remoto ha restituito un errore: non trovato Quando premo il pulsante Inserisci per inserire un nuovo record in 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;
}
}
. Soluzione
Stai inviando dati JSON, ma stai dicendo che è un tipo di contenuto diverso:
wc.Headers["Content-Type"] = "application/x-www-form-urlencoded";
.
Impostare il tipo di contenuto corretto nella richiesta:
wc.Headers["Content-Type"] = "application/json";
.
Qualcosa non correlato: se il tuo tipo non è decorato con [DataContract]
, non è necessario decorare la colonna Proprietà1 e la colonna2 con [DataMember]
.
Altri suggerimenti
Prova a lanciare arg.Erro
r a WebException
e controllare il codice statuto.Potrebbe essere 401 (non autorizzato)
var webException = arg.Error as WebException;
if(webException == null) return;
if (webException.Response != null)
{
var response = (HttpWebResponse)webException.Response;
var status = response.StatusCode; //press F9 here
}
.