我正在使用Xamarin iOS和iOS开发iOS应用程序MonoGame.我想通过他们的REST API使用Parse的推送通知,所以首先我必须创建一个安装对象:
var bundle = new Dictionary<string, object>();
bundle.Add("channels", "");
bundle.Add("deviceType", "ios");
bundle.Add("deviceToken", _deviceToken);
string urlpath = "https://api.parse.com/1/installations";
var httpWebRequest = (HttpWebRequest)WebRequest.Create(urlpath);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Headers.Add("X-Parse-Application-Id", _parseAppID);
httpWebRequest.Headers.Add("X-Parse-REST-API-KEY", _parseRestAPIKey);
httpWebRequest.Method = "POST";
string bundleString = bundle.ToJson();
byte[] buffer = Encoding.GetEncoding("UTF-8").GetBytes(bundleString);
string result = Convert.ToBase64String(buffer);
StreamWriter requestWriter = new StreamWriter(httpWebRequest.GetRequestStream());
requestWriter.Write(result, 0, result.Length);
requestWriter.Close();
WebResponse httpResponse = await httpWebRequest.GetResponseAsync();
Stream stream = httpResponse.GetResponseStream();
string json = string.Empty;
using (StreamReader reader = new StreamReader(stream))
{
while (!reader.EndOfStream)
{
json += reader.ReadLine();
}
}
JsonObject jsonObject = JsonObject.Parse(json);
_varStorage.Save("ObjectId", jsonObject.Get<string>("objectId"));
bundleString值是:
“{\”channels\”:\”\”,\”deviceType\”:\”ios\”,\”deviceToken\”:\”46becd0a165be042eeab5a1ec96b8858065cbea7311479da16c0fd1c9428e2eb\”}”
此代码引发System.Net.WebExceptionStatus.ProtocolError错误400“错误请求”,我不明白为什么.
最佳答案 根据文档
https://www.parse.com/docs/rest#installations,通道应该是一个字符串数组
bundle.Add("channels", new [] { "" });