C# windows phone 8中的toast通知-流错误
我已从microsoft站点下载了toast通知示例代码,但代码执行不正确--在httpwebresponse中出现错误--C# windows phone 8中的toast通知-流错误,c#,windows-phone-8,C#,Windows Phone 8,我已从microsoft站点下载了toast通知示例代码,但代码执行不正确--在httpwebresponse中出现错误-- protectedvoid按钮sendtoos\u单击(对象发送者,事件参数e) { 尝试 { //获取Microsoft推送通知服务在创建通知通道时返回给推送客户端的Uri。 //通常,web服务将侦听来自web客户端的Uri,并维护要发送的Uri列表 //通知发送到。 string subscriptionUri=TextBoxUri.Text.ToString();
protectedvoid按钮sendtoos\u单击(对象发送者,事件参数e)
{
尝试
{
//获取Microsoft推送通知服务在创建通知通道时返回给推送客户端的Uri。
//通常,web服务将侦听来自web客户端的Uri,并维护要发送的Uri列表
//通知发送到。
string subscriptionUri=TextBoxUri.Text.ToString();
HttpWebRequest sendNotificationRequest=(HttpWebRequest)WebRequest.Create(subscriptionUri);
//我们将创建一个HTTPWebRequest,将toast通知发布到Microsoft推送通知服务。
//HTTP POST是唯一允许发送通知的方法。
sendNotificationRequest.Method=“POST”;
//可选的自定义标头X-MessageID唯一标识通知消息。
//如果存在,则在通知响应中返回//相同的值。该值必须是包含UUID的字符串。
//sendNotificationRequest.Headers.Add(“X-MessageID”,即“);
//创建toast消息。
字符串toastMessage=“”+
"" +
"" +
“”+TextBoxTitle.Text.ToString()+“”+
“”+TextBoxSubTitle.Text.ToString()+“”+
“/Page2.xaml?NavigatedFrom=Toast通知”+
" " +
"";
//设置要发送的通知有效负载。
byte[]notificationMessage=Encoding.Default.GetBytes(toastMessage);
//设置web请求内容长度。
sendNotificationRequest.ContentLength=notificationMessage.Length;
sendNotificationRequest.ContentType=“text/xml”;
sendNotificationRequest.Headers.Add(“X-WindowsPhone-Target”、“toast”);
sendNotificationRequest.Headers.Add(“X-NotificationClass”,“2”);
使用(Stream requestStream=sendNotificationRequest.GetRequestStream())
{
Write(notificationMessage,0,notificationMessage.Length);
}
//发送通知并获取响应。
HttpWebResponse=(HttpWebResponse)sendNotificationRequest.GetResponse();//此行出现异常
字符串notificationStatus=response.Headers[“X-notificationStatus”];
字符串notificationChannelStatus=response.Headers[“X-SubscriptionStatus”];
字符串deviceConnectionStatus=response.Headers[“X-deviceConnectionStatus”];
//显示来自Microsoft推送通知服务的响应。
//通常,错误处理代码在这里。在现实世界中,由于数据连接并不总是可用,
//如果无法访问设备,则可能需要限制通知。
Text=notificationStatus+“|”+设备连接状态+“|”+notificationChannelStatus;
}
捕获(例外情况除外)
{
TextBoxResponse.Text=“发送更新时发现异常:”+ex.ToString();
}
}
}
我曾尝试在VS2010和VS2013 express中运行此代码,但仍然出现相同的错误
protected void ButtonSendToast_Click(object sender, EventArgs e)
{
try
{
// Get the Uri that the Microsoft Push Notification Service returns to the Push Client when creating a notification channel.
// Normally, a web service would listen for Uri's coming from the web client and maintain a list of Uri's to send
// notifications out to.
string subscriptionUri = TextBoxUri.Text.ToString();
HttpWebRequest sendNotificationRequest = (HttpWebRequest)WebRequest.Create(subscriptionUri);
// We will create a HTTPWebRequest that posts the toast notification to the Microsoft Push Notification Service.
// HTTP POST is the only allowed method to send the notification.
sendNotificationRequest.Method = "POST";
// The optional custom header X-MessageID uniquely identifies a notification message.
// If it is present, the // same value is returned in the notification response. It must be a string that contains a UUID.
// sendNotificationRequest.Headers.Add("X-MessageID", "<UUID>");
// Create the toast message.
string toastMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<wp:Notification xmlns:wp=\"WPNotification\">" +
"<wp:Toast>" +
"<wp:Text1>" + TextBoxTitle.Text.ToString() + "</wp:Text1>" +
"<wp:Text2>" + TextBoxSubTitle.Text.ToString() + "</wp:Text2>" +
"<wp:Param>/Page2.xaml?NavigatedFrom=Toast Notification</wp:Param>" +
"</wp:Toast> " +
"</wp:Notification>";
// Sets the notification payload to send.
byte[] notificationMessage = Encoding.Default.GetBytes(toastMessage);
// Sets the web request content length.
sendNotificationRequest.ContentLength = notificationMessage.Length;
sendNotificationRequest.ContentType = "text/xml";
sendNotificationRequest.Headers.Add("X-WindowsPhone-Target", "toast");
sendNotificationRequest.Headers.Add("X-NotificationClass", "2");
using (Stream requestStream = sendNotificationRequest.GetRequestStream())
{
requestStream.Write(notificationMessage, 0, notificationMessage.Length);
}
// Send the notification and get the response.
HttpWebResponse response = (HttpWebResponse)sendNotificationRequest.GetResponse(); // Exception in this line
string notificationStatus = response.Headers["X-NotificationStatus"];
string notificationChannelStatus = response.Headers["X-SubscriptionStatus"];
string deviceConnectionStatus = response.Headers["X-DeviceConnectionStatus"];
// Display the response from the Microsoft Push Notification Service.
// Normally, error handling code would be here. In the real world, because data connections are not always available,
// notifications may need to be throttled back if the device cannot be reached.
TextBoxResponse.Text = notificationStatus + " | " + deviceConnectionStatus + " | " + notificationChannelStatus;
}
catch (Exception ex)
{
TextBoxResponse.Text = "Exception caught sending update: " + ex.ToString();
}
}