1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Net;
using System.IO;

namespace PushSharp.Blackberry
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            pushToWidget("Hi Anoop If You will Get Notifcation please miss call me 8130513899");

        }



       public bool pushToWidget(string pushedMessage)
        {
           // String BESAddress = "https://pushapi.eval.blackberry.com/mss/PD_pushRequest";
            //String BESWebserverListenPort = "pushPort";
            String widgetNotificationUrl = "https://pushapi.eval.blackberry.com/mss/PD_pushRequest";
            String pushUserName = "3457-
B730k4394m49rOr96r33M8t74303c51k728";
            String pushPassword = "smnBIWO3";


          //  String pushPort = "33215";
            string Boundary = "Boundary ";
            string DeliverBefore = DateTime.UtcNow.AddMinutes(5).ToString("s", System.Globalization.CultureInfo.InvariantCulture) + "Z";
            Response.Write(DeliverBefore);
            bool success = true;
            StringBuilder Data = new StringBuilder();
            Data.AppendLine("--" + Boundary);
            Data.AppendLine("Content-Type: application/xml; charset=utf-8");
            Data.AppendLine("");
            Data.AppendLine("<?xml version=\"1.0\"?>");
            Data.AppendLine("<!DOCTYPE pap PUBLIC \"-//WAPFORUM//DTD PAP 2.1//EN\">");
            Data.AppendLine("<pap>");
            Data.AppendLine("<push-message push-id=" + (char)34 + ID + (char)34 + " deliver-before-timestamp=" +

(char)34 + DeliverBefore + (char)34 + " source-reference=" + (char)34 + pushUserName + (char)34 + ">");
            Data.AppendLine("<address address-value=\"" + "push_all" + "\"/>");
            Data.AppendLine("<quality-of-service delivery-method=\"unconfirmed\"/>");
            Data.AppendLine("</push-message>");
            Data.AppendLine("</pap>");
            Data.AppendLine("--" + Boundary);
            Data.AppendLine("Content-Type: text/plain");
            Data.AppendLine("Push-Message-ID: " + ID);
            Data.AppendLine("");
            Data.AppendLine(pushedMessage);
            Data.AppendLine("--" + Boundary + "--");
            Data.AppendLine("");
            byte[] bytes = Encoding.ASCII.GetBytes(Data.ToString());

            Stream requestStream = null;
            HttpWebResponse HttpWRes = null;
            HttpWebRequest HttpWReq = null;

            try
            {
                //http://<BESName>:<BESPort>/push?DESTINATTION=<PIN/EMAIL>&PORT=<PushPort>&REQUESTURI=/
               // Build the URL to define our connection to the BES.

                String BESName = "cp3457.pushapi.eval.blackberry.com/mss/PD_pushRequest";

                string httpURL = "https://" + BESName + "/push?DESTINATION=2B838E45&PORT=33215&REQUESTURI=/";

             //   string httpURL = BESAddress + ":" + BESWebserverListenPort+ "/push?DESTINATION=" + pushPin + "&PORT=" + pushPort+ "&REQUESTURI=/";

                //make the connection
                HttpWReq = (HttpWebRequest)WebRequest.Create(httpURL);
                HttpWReq.Method = ("POST");
                //add the headers nessecary for the push
                HttpWReq.ContentType = "text/plain";
                HttpWReq.ContentLength = bytes.Length;
                // ******* Test this *******
                HttpWReq.Headers.Add("X-Rim-Push-Id", "push_all" + "~" + DateTime.Now); //"~" +pushedMessage +
                HttpWReq.Headers.Add("X-Rim-Push-Reliability", "application-preferred");
                HttpWReq.Headers.Add("X-Rim-Push-NotifyURL", (widgetNotificationUrl + "push_all" + "~" + pushedMessage + "~" + DateTime.Now).Replace(" ", ""));

                // *************************

               // HttpWReq.Credentials = new MDSCredentials(pushUserName, pushPassword);
                HttpWReq.Credentials = new NetworkCredential(pushUserName, pushPassword);

                Console.WriteLine(pushedMessage);
                requestStream = HttpWReq.GetRequestStream();
                //Write the data from the source
                requestStream.Write(bytes, 0, bytes.Length);
                requestStream.Close();
                //get the response
                HttpWRes = (HttpWebResponse)HttpWReq.GetResponse();

                //if the MDS received the push parameters correctly it will either respond with okay or accepted
                if (HttpWRes.StatusCode == HttpStatusCode.OK || HttpWRes.StatusCode == HttpStatusCode.Accepted)
                {
                    success = true;
                }
                else
                {
                    success = false;
                }
                //Close the streams

                HttpWRes.Close();
                requestStream.Close();
            }
            catch (System.Exception e)
            {
                success = false;
            }

            return success;
        }


    }
}

请检查此代码,代码运行良好并获得响应成功,但当我使用此代码时无法发送

在php中使用C#作为服务器端的黑莓推送通知 它工作正常并收到通知我不知道我在哪里做错了

4

1 回答 1

0

我设法解决了这个问题。您的代码中的主要问题是您用于发布Push Message. 据我了解,您实际上是在接收消息的设备中使用 BIS,而不是 BES。因此,httpURL您正在使用的 可以编辑为String httpURL = "https://cpxxxx.pushapi.eval.blackberry.com/mss/PD_pushRequest";. 完整的工作代码如下:

private void pushMessageSample(string pushedMessage)
{
String appid="xxxx-xxxxxxxxxxxxxxxxxxxxxxxxxx";
String password = "xxxxxx";
String deliverbefore = DateTime.UtcNow.AddMinutes(5).ToString("s",System.Globalization.CultureInfo.InvariantCulture) + "Z";
String pushPin = "xxxxxxxx";
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);
tRequest.Method = "POST";
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();
}

public static void SetBasicAuthHeader(WebRequest req, String appID, String userPassword)
{
string authInfo = appID + ":" + userPassword;
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
req.Headers["Authorization"] = "Basic " + authInfo;
}

该方法SetBasicAuthHeader是对用户的访问进行身份验证以发出POST请求。

于 2013-06-26T05:58:20.720 回答