0

我正在尝试构建用于发送推送通知(toast 类型)的 httpmessage,但它无法识别以下代码中的方法。我在类库中有这段代码。

在 sendNotificationRequest.Headers.Add("X-NotificationClass", "2"); 中添加方法

sendNotificationRequest.ContentLength 中的内容长度 = notificationMessage.Length;

流中的GetRequestStream requestStream = sendNotificationRequest.GetRequestStream()

HttpWebResponse 中的 GetResponse 响应 = (HttpWebResponse)sendNotificationRequest.GetResponse();

        HttpWebRequest sendNotificationRequest = (HttpWebRequest)WebRequest.Create(channel.ChannelUri.ToString());
        sendNotificationRequest.Method = "POST";
        //Indicate that you'll send toast notifications!
        sendNotificationRequest.ContentType = "text/xml";
        sendNotificationRequest.Headers = new WebHeaderCollection();
        sendNotificationRequest.Headers.Add("X-NotificationClass", "2");
        if (string.IsNullOrEmpty(txtMessage.Text)) return;

        //Create xml envelope
        string data = "X-WindowsPhone-Target: toast\r\n\r\n" +
                "<?xml version='1.0' encoding='utf-8'?>" +
                "<wp:Notification xmlns:wp='WPNotification'>" +
                "<wp:Toast>" +
                "<wp:Text1>{0}</wp:Text1>" +
                "</wp:Toast>" +
                "</wp:Notification>";

        //Wrap custom data into envelope
        string message = string.Format(data, txtMessage.Text);
        byte[] notificationMessage = Encoding.Default.GetBytes(message);

        // Set Content Length
        sendNotificationRequest.ContentLength = notificationMessage.Length;

        //Push data to stream
        using (Stream requestStream = sendNotificationRequest.GetRequestStream())
        {
            requestStream.Write(notificationMessage, 0, notificationMessage.Length);
        }

        //Get reponse for message sending
        HttpWebResponse response = (HttpWebResponse)sendNotificationRequest.GetResponse();
        string notificationStatus = response.Headers["X-NotificationStatus"];
        string notificationChannelStatus = response.Headers["X-SubscriptionStatus"];
        string deviceConnectionStatus = response.Headers["X-DeviceConnectionStatus"];
4

1 回答 1

0

将代码移动到 WCF 项目后,它可以工作了。

于 2013-04-30T06:01:27.163 回答