我想通过parse.com发送推送通知,我有像这样的网络服务
[的WebMethod] public bool pushNotification(string pushMessage) { bool isPushMessageSend = false;
string postString = "";
string urlpath = "https://api.parse.com/1/push";
//var httpWebRequest = (HttpWebRequest)WebRequest.Create(urlpath);
System.Net.WebRequest request = System.Net.HttpWebRequest.Create(urlpath);
request.UseDefaultCredentials = true;
request.PreAuthenticate = true;
request.Credentials = CredentialCache.DefaultCredentials;
postString = "{ \"channels\": [ \"Trials\" ], " +
"\"data\" : {\"alert\":\"" + pushMessage + "\"}" +
"}";
request.ContentType = "application/json";
request.ContentLength = postString.Length;
request.Headers.Add("APP ID", "My Parse App Id");
request.Headers.Add("REST API KEY", "My Rest API Key");
request.Method = "POST";
StreamWriter requestWriter = new StreamWriter(request.GetRequestStream());
requestWriter.Write(postString);
requestWriter.Close();
//var httpResponse = (HttpWebResponse)request.GetResponse();
System.Net.WebResponse response = request.GetResponse();
using (var streamReader = new StreamReader(response.GetResponseStream()))
{
var responseText = streamReader.ReadToEnd();
JObject jObjRes = JObject.Parse(responseText);
if (Convert.ToString(jObjRes).IndexOf("true") != -1)
{
isPushMessageSend = true;
}
}
return isPushMessageSend;
}
我有这样的错误
System.Net.WebException: The remote server returned an error: (401) Unauthorized.
at System.Net.HttpWebRequest.GetResponse()
at WebService.pushNotification(String pushMessage) in c:\Users\Hurkan Seyhan\desktop\website3\App_Code\WebService.cs:line 488
我该如何解决这个问题?
答案 0 :(得分:1)
根据他们的文档(https://parse.com/docs/push_guide#sending-channels/REST)我认为你的标题中存在错误
而不是
request.Headers.Add("[YOUR APP ID]", "My Parse App Id");
request.Headers.Add("[YOUR REST API KEY]", "My Rest API Key");
您可能应该写下以下内容:
request.Headers.Add("X-Parse-Application-Id", "[YOUR APP ID]");
request.Headers.Add("X-Parse-REST-API-Key", "[YOUR REST API KEY]");