远程服务器返回错误:(401) Unauthorized: Push Notification

Posted

技术标签:

【中文标题】远程服务器返回错误:(401) Unauthorized: Push Notification【英文标题】:The remote server returned an error: (401) Unauthorized: Push Notification 【发布时间】:2013-12-03 14:04:25 【问题描述】:

我正在开发 BB 10 级联中的推送通知。我正在开发自己的推送发起程序,而不是使用推送服务 SDK。我的服务器端推送启动器代码如下。

private static void pushMessageSample(string pushPin, string pushedMessage)

string s = "";
try
String appid = "xxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
String password = "xxxxxxxx";
String deliverbefore = DateTime.UtcNow.AddMinutes(5).ToString("s",     System.Globalization.CultureInfo.InvariantCulture) + "Z";
String Boundary = "mPsbVQo0a68eIL3OAxnm";

StringBuilder dataToSend = new StringBuilder();

dataToSend.AppendLine("--" + Boundary);
dataToSend.AppendLine("Content-Type: application/xml; charset=UTF-8");

dataToSend.AppendLine("");
dataToSend.AppendLine("<?xml version=\"1.0\"?>");
dataToSend.AppendLine("<!DOCTYPE pap PUBLIC \"-//WAPFORUM//DTD PAP 2.1//EN\" \"http://www.openmobilealliance.org/tech/DTD/pap_2.1.dtd\">");
dataToSend.AppendLine("<pap>");
string myPushId = DateTime.Now.ToFileTime().ToString();
dataToSend.AppendLine("<push-message push-id=" + (char)34 + myPushId + (char)34 + " deliver-before-timestamp=" +
 (char)34 + deliverbefore + (char)34 + " source-reference=" + (char)34 + appid + (char)34 + ">");
//dataToSend.AppendLine("<push-message push-id=\"" + myPushId + "\" source-reference=\"" + appid + "\">");
dataToSend.AppendLine("<address address-value=\"" + pushPin + "\"/>");
dataToSend.AppendLine("<quality-of-service delivery-method=\"unconfirmed\"/>");
dataToSend.AppendLine("</push-message>");
dataToSend.AppendLine("</pap>");
dataToSend.AppendLine("--" + Boundary);

dataToSend.AppendLine("Content-Type: text/plain");
dataToSend.AppendLine("Push-Message-ID: " + myPushId);
dataToSend.AppendLine("");

dataToSend.AppendLine(pushedMessage);

dataToSend.AppendLine("--" + Boundary + "--");
dataToSend.AppendLine("");

byte[] bytes = Encoding.ASCII.GetBytes(dataToSend.ToString());
String httpURL = "https://cpxxxx.pushapi.eval.blackberry.com/mss/PD_pushRequest";

WebRequest tRequest;
tRequest = WebRequest.Create(httpURL);
//SetProxy(tRequest);
tRequest.Method = "POST";
//tRequest.ContentType = "text/plain";

//tRequest.ContentLength = bytes.Length;
tRequest.Credentials = new NetworkCredential(appid, password);

tRequest.PreAuthenticate = true;
tRequest.ContentType = "multipart/related; boundary=" + Boundary + ";             type=application/xml";
tRequest.ContentLength = bytes.Length;
string rawCredentials = string.Format("0:1", appid, password);
tRequest.Headers.Add("Authorization",
    string.Format(
        "Basic 0",
        Convert.ToBase64String(Encoding.UTF8.GetBytes(rawCredentials))));

SetBasicAuthHeader(tRequest, appid, password);

Stream dataStream = tRequest.GetRequestStream();
dataStream.Write(bytes, 0, bytes.Length);
dataStream.Close();

WebResponse tResponse = tRequest.GetResponse();

dataStream = tResponse.GetResponseStream();

StreamReader tReader = new StreamReader(dataStream);

String sResponseFromServer = tReader.ReadToEnd();

tReader.Close();
dataStream.Close();
tResponse.Close();

catch (Exception ex1)

    s = ex1.Message.ToString();


但我收到远程服务器返回错误:(401) Unauthorized 错误。如何解决?

【问题讨论】:

【参考方案1】:

对于生产:使用https://cpxxxx.pushapi.na.blackberry.com/mss/PD_pushRequest。对于测试,您可以使用 pushapi.eval.blackberry.com

【讨论】:

以上是关于远程服务器返回错误:(401) Unauthorized: Push Notification的主要内容,如果未能解决你的问题,请参考以下文章

远程服务器返回错误:(401) Unauthorized: Push Notification

远程服务器返回错误:(401)在Zendesk中未经授权

远程服务器返回错误:(401) Unauthorized with paypal

远程服务器返回错误:(401) Unauthorized。使用 Paypal Rest APi 时

C#使用HttpWebRequest请求url出现远程服务器返回一个错误:401未经授权

ReportViewer中的SSRS导出返回401 Unauthorized