1

我已经构建了一个简单的 BlackBerry 应用程序,现在我想在其中实现一个小聊天功能。这个想法是通过网络服务向服务器发送消息。这已成功实施。我现在需要在我的 BlackBerry 应用程序中接收回复。成功捕获服务器的响应。如何将此响应推送到黑莓手机。我已通过电子邮件中收到的评估凭证在我的 BlackBerry 中成功注册 Push Service。我用 C# 编写了一个简单的服务器端代码,但它从来没有工作过。下面是我的 C# 代码。任何人都可以指导我吗?我的设备从未收到推送消息。可能是我做错了。

请帮忙!

public bool pushToWidget(string pushedMessage, string pushPin)
    {
        String BESAddress = "https://pushapi.eval.blackberry.com/mss/PD_pushRequest";
        String BESWebserverListenPort = "106"; //random
        String widgetNotificationUrl = "https://pushapi.eval.blackberry.com/mss/PD_pushRequest";
        String pushUserName = "xxx";
        String pushPassword = "xxx";
        String pushPort = "xxxx ";
        string Boundary = "Boundary ";
        String ID = "xxx-xxxx";
        string DeliverBefore = DateTime.UtcNow.AddMinutes(60).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=\"" + pushPin + "\"/>");
        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
        { 
            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", pushPin + "~" + DateTime.Now); //"~" + pushedMessage +
            // HttpWReq.Headers.Add("X-Rim-Push-Reliability", "application-preferred");
            HttpWReq.Headers.Add("X-Rim-Push-NotifyURL", (widgetNotificationUrl + 
                pushPin + "~" + pushedMessage + "~" + DateTime.Now).Replace(" ", ""));

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

            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();
            String response = HttpWRes.ToString();

            //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;
    }
4

0 回答 0