문제

Azure에서 새로운 모바일 서비스를 만들려고 노력합니다. 그리고 JSON이 데이터가 올바르게 노출되었습니다.

https://lifeholo.zure-mobile.net/tables/userpf <./ P>

userpf는 샘플 테이블입니다.

질문을 단순화하기 위해 방금 "모두"권한을 수정했습니다.

아래에 나열된 코드가 작동하지 않는 문제입니다. 오류 메시지는 다음과 같습니다. 원격 서버가 오류를 반환했습니다 : 발견되지 않음 insert 버튼을 누르면 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;
        }
    }
.

도움이 되었습니까?

해결책

JSON 데이터를 보내고 있지만 다른 콘텐츠 유형이라고 말하고 있습니다.

wc.Headers["Content-Type"] = "application/x-www-form-urlencoded"; 
.

요청에서 올바른 내용 유형을 설정합니다.

wc.Headers["Content-Type"] = "application/json"; 
.

무언가가없는 것 : 유형이 [DataContract]로 장식되지 않은 경우 Properties Column1과 Column2를 [DataMember]로 장식 할 필요가 없습니다.

다른 팁

arg.Error를 WebException로 던지고 통합 코드를 확인하십시오.그것은 401 (무단)일지도 모릅니다

 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
   }
.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top