0

我正在尝试向我的 BlackBerry 应用程序发送推送消息。php黑莓通过服务器端代码成功接收到消息。但是,我在 C# 服务器端代码上苦苦挣扎了很长一段时间。以下是我正在运行的代码:

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

    public static void SetProxy(WebRequest req)
    {
        Uri newUri = new Uri("http://cp(AppID).pushapi.eval.blackberry.com");
        WebProxy proxy = new WebProxy();
        proxy.Address = newUri;
        proxy.Credentials = new NetworkCredential("<appID>", "<password>");

        req.Proxy = proxy;
    }

    public bool pushTest(string msg)
    {
        HttpWebResponse HttpWRes = null;
        HttpWebRequest HttpWReq = null;

        string pin = "xxxxxxxx"; // or actual pin of device
        string applicationID = "xxxxxxxxxxxxxxxxxxxxxx";
        string BOUNDARY = "mPsbVQo0a68eIL3OAxnm";
        //string msg1 = "testing c#"; // the message to send

        string userName = "<appID>";
        string userPW = "<password>";

        string url = "https://pushapi.eval.blackberry.com/mss/PD_pushRequest";

        HttpWReq = (HttpWebRequest)WebRequest.Create(url);

        SetProxy(HttpWReq);

        HttpWReq.Method = ("POST");
        HttpWReq.Accept = "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2";

        //HttpWReq.Credentials = new NetworkCredential(userName, userPW);

        HttpWReq.PreAuthenticate = true;
        HttpWReq.ContentType = "multipart/related; boundary=" + BOUNDARY + "; type=application/xml";

        SetBasicAuthHeader(HttpWReq, userName, userPW);

        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=\"" + myPushId + "\" source-reference=\"" + applicationID + "\">");
        dataToSend.AppendLine("<address address-value=\"" + pin + "\"/>");
        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(msg);

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

        Stream requestStream = null;
        string pushResult = "";
        try
        {
            requestStream = HttpWReq.GetRequestStream();
        }
        catch (Exception ex)
        {
            pushResult = "Push failed! " + ex.ToString();
        }
        byte[] outStr = new ASCIIEncoding().GetBytes(dataToSend.ToString());
        requestStream.Write(outStr, 0, outStr.Length);
        requestStream.Close();

        try
        {
            HttpWRes = (HttpWebResponse)HttpWReq.GetResponse();
            // MessageBox.show(""+HttpWRes);

        }
        catch (Exception ex)
        {
            //push failed
        }

        if (HttpWRes != null)
        {
            HttpWRes.Close();
        }

        return true;
    }

我收到以下错误:

Push failed! System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 68.171.224.60:80
   at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
   at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception)
   --- End of inner exception stack trace ---
   at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)
   at System.Net.HttpWebRequest.GetRequestStream()

应用的凭据或 URL 是否有任何问题?请帮忙!!

4

2 回答 2

1

我设法解决了这个问题。上述代码中的主要问题是构造Http请求。

WebRequest tRequest;
tRequest = WebRequest.Create(httpURL);
tRequest.Method = "POST";
tRequest.Credentials = new NetworkCredential(appid, password);

另一个需要处理的重要事情是Http Response 401通过上述方式发出请求时发生的情况NetworkCredentials。为了处理这个问题,在发出请求时应该调用以下方法:

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

完整的服务器端代码Push Message如下:

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();
}
于 2013-06-26T05:36:10.407 回答
0

好奇你为什么注释掉代码的 HttpsWReq.Credentials = new NetowrkCredential("username", "password") 部分?

try-catch 语句正在执行 HttpWReq.GetRequestStream(); 虽然一切看起来都在那里。另一个问题可能是调用 SetProxy 的转换和方法参数。这是期望 WebRequest 类型。

HttpWReq = (HttpWebRequest)WebRequest.Create(url);

    SetProxy(HttpWReq);

你什么时候申请你的证书(评估/生产?

于 2013-06-17T18:02:16.730 回答