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

本文关键字:发送 JSON- 错误 AppEngine POST 中从 WP8 | 更新日期: 2023-09-27 18:00:22

嗨,我正在尝试将以下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"answers"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>

我应该怎么做,才能收到想要的"OK"响应?

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

好的,所以问题是我的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);
    }