且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用C#作为服务器端的黑莓推送通知

更新时间:2023-02-26 21:18:13

我得到了同样的错误,当我尝试了上述的code。替换

 字符串BESName =CP **** pushapi.eval.blackberry.com

通过

 字符串BESName =cpxxx.pushapi.eval.blackberry.com/mss/PD_pushRequest;

,并确保您在这里提供正确的用户名和密码:

  HttpWReq.Credentials =新的NetworkCredential(用户名,密码);

我的成功= TRUE;

不过,即使上述code成功执行,我还没有看到BlackBerry设备上推送消息。

  1. I am trying to send push notification in blackberry by using C# Web service but i am facing problem is it return exception "The remote server returned an error: (404) Not Found.". all info is correct as per RIM Standard so please Help me ASAP.

     [WebMethod]
    public bool push(string notification)
    {
        bool success = true;
        byte[] bytes = Encoding.ASCII.GetBytes("Hello");
    
        Stream requestStream = null;
        HttpWebResponse HttpWRes = null;
        HttpWebRequest HttpWReq = null;
    
    
        String BESName = "cp****.pushapi.eval.blackberry.com";
        try
        {
            //http://<BESName>:<BESPort>/push?DESTINATTION=<PIN/EMAIL>&PORT=<PushPort>&REQUESTURI=/
            // Build the URL to define our connection to the BES.
            string httpURL = "https://" + BESName +  "/push?DESTINATION=2B838E45&PORT=32721&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", "2B838E45" + "~" + DateTime.Now); //"~" +pushedMessage +
            HttpWReq.Headers.Add("X-Rim-Push-Reliability", "application-preferred");
            HttpWReq.Headers.Add("X-Rim-Push-NotifyURL", ("http://" + BESName + "2B838E45~Hello~" + DateTime.Now).Replace(" ", ""));
    
            // *************************
            HttpWReq.Credentials = new NetworkCredential("Username", "Password");
    
            requestStream = HttpWReq.GetRequestStream();
            //Write the data from the source
            requestStream.Write(bytes, 0, bytes.Length);
    
            //get the response
            HttpWRes = (HttpWebResponse)HttpWReq.GetResponse();
    
            var pushStatus = HttpWRes.Headers["X-RIM-Push-Status"];
    
            //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)
        {
            success = false;
        }
    
        return success;
    }
    

I got the same error when I tried your code above. Replace

String BESName = "cp****.pushapi.eval.blackberry.com";

With

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

and make sure you provide the right username and password here:

HttpWReq.Credentials = new NetworkCredential("username", "password");

I got success = true;

However, even though the above code executed successfully, I still do not see the push message on the BlackBerry device.