在POST中将JSON从WP8发送到AppEngine - 错误500

时间:2013-02-20 19:33:08

标签: c# json google-app-engine http-post windows-phone-8

您好我正在尝试将以下JSON字符串发送到AppEngine服务器。该字符串如下所示:

{"param2":50.0,"param1":50.0,"additionalParams":{"param3":"123","userID":"1234561"}}

我用来发送它的代码如下:

public async Task<string> SendJSONData(string urlToCall, string JSONData)
    {
        // server to POST to
        string url = urlToCall;

        // HTTP web request
        var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
        httpWebRequest.ContentType = "action";
        httpWebRequest.Method = "POST";

        // Write the request Asynchronously 
        using (var stream = await Task.Factory.FromAsync<Stream>(httpWebRequest.BeginGetRequestStream,
                                                                 httpWebRequest.EndGetRequestStream, null))
        {
            //create some json string
            string json = JSONData;

            // convert json to byte array
            byte[] jsonAsBytes = Encoding.UTF8.GetBytes(json);

            // Write the bytes to the stream
            await stream.WriteAsync(jsonAsBytes, 0, jsonAsBytes.Length);
        }

        WebResponse response = await httpWebRequest.GetResponseAsync();
        StreamReader requestReader = new StreamReader(response.GetResponseStream());
        String webResponse = requestReader.ReadToEnd();
        return webResponse;
}

我使用Fiddler嗅探了发送到服务器的内容:

POST http://server.appspot.com/method HTTP/1.1
Accept: */*
Content-Length: 85
Accept-Encoding: identity
Content-Type: action
User-Agent: NativeHost
Host: server.appspot.com
Connection: Keep-Alive
Cache-Control: no-cache
Pragma: no-cache

{"param2":50.0,"param1":50.0,"additionalParams":{"param3":"123","userID":"1234561"}}

请注意我已经使用“Content-Type”参数进行了预期,将其设置为“text / plain”和“application / json”。 服务器的答案仍然如下:

HTTP/1.1 500 Internal Server Error
Date: Wed, 20 Feb 2013 18:54:34 GMT
Content-Type: text/html; charset=UTF-8
Server: Google Frontend
Content-Length: 466


<html><head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>500 Server Error</title>
</head>
<body text=#000000 bgcolor=#ffffff>
<h1>Error: Server Error</h1>
<h2>The server encountered an error and could not complete your request.<p>If the            problem persists, please <A HREF="http://code.google.com/appengine/community.html">report</A> your problem and mention this error message and the query that caused it.</h2>

我该怎么办,才能收到所需的“确定”回复?

1 个答案:

答案 0 :(得分:0)

好的问题是我的POST中缺少“action”参数。 解决方法如下所示:

    // Write the request Asynchronously 
    using (var stream = await Task.Factory.FromAsync<Stream>
    (httpWebRequest.BeginGetRequestStream,httpWebRequest.EndGetRequestStream, null))
    {
        //create some json string
        string json = "action="+JSONData;

        // convert json to byte array
        byte[] jsonAsBytes = Encoding.UTF8.GetBytes(json);

        // Write the bytes to the stream
        await stream.WriteAsync(jsonAsBytes, 0, jsonAsBytes.Length);
    }