C# 在POST中将JSON从WP8发送到AppEngine-错误500
您好,我正在尝试将以下JSON字符串发送到AppEngine服务器。字符串如下所示:C# 在POST中将JSON从WP8发送到AppEngine-错误500,c#,json,google-app-engine,http-post,windows-phone-8,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 PO
{"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;
}
请注意,我已经体验过“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>
// 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);
}
HTTP/1.1500内部服务器错误
日期:2013年2月20日星期三18:54:34 GMT
内容类型:text/html;字符集=UTF-8
服务器:谷歌前端
内容长度:466
500服务器错误
错误:服务器错误
服务器遇到错误,无法完成您的请求。如果问题仍然存在,请说明您的问题,并说明此错误消息和导致此问题的查询。
我应该怎么做,才能收到所需的“OK”响应?OK所以问题在于我的帖子中缺少“action”参数。 解决方法如下所示:
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>
// 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);
}
//异步写入请求
使用(var stream=await Task.Factory.fromsync)
(httpWebRequest.BeginGetRequestStream,httpWebRequest.EndGetRequestStream,null))
{
//创建一些json字符串
string json=“action=”+JSONData;
//将json转换为字节数组
byte[]jsonAsBytes=Encoding.UTF8.GetBytes(json);
//将字节写入流
wait stream.WriteAsync(jsonAsBytes,0,jsonAsBytes.Length);
}