C# 类型为';System.UriFormatException';发生在System.dll中,但未在用户代码中处理
我每次运行应用程序时都会遇到此错误,uri会生成,但当我试图通过单击aspn网页上的按钮发送toast通知时,程序崩溃并显示该错误(System.dll中出现“System.UriFormatException”类型的异常,但未在用户代码中处理)。以下是我的代码线程:[在HttpWebRequest请求=(HttpWebRequest)WebRequest.Create(channelURI);行上崩溃]C# 类型为';System.UriFormatException';发生在System.dll中,但未在用户代码中处理,c#,httpwebrequest,uri,C#,Httpwebrequest,Uri,我每次运行应用程序时都会遇到此错误,uri会生成,但当我试图通过单击aspn网页上的按钮发送toast通知时,程序崩溃并显示该错误(System.dll中出现“System.UriFormatException”类型的异常,但未在用户代码中处理)。以下是我的代码线程:[在HttpWebRequest请求=(HttpWebRequest)WebRequest.Create(channelURI);行上崩溃] 公共字符串发送通知(字符串消息) { string channelURI=“将您的频道放在
公共字符串发送通知(字符串消息)
{
string channelURI=“将您的频道放在这里”;
HttpWebRequest请求=(HttpWebRequest)WebRequest.Create(channelURI);
request.Method=“POST”;
request.ContentType=“text/xml”;
添加(“X-NotificationClass”、“2”);
添加(“X-WindowsPhone-Target”、“toast”);
字符串notificationData=“”+
"" +
"" +
“WP7吐司”+
“”+消息+“”+
"" +
"";
byte[]contents=Encoding.Default.GetBytes(notificationData);
request.ContentLength=contents.Length;
使用(Stream requestStream=request.GetRequestStream())
{
Write(contents,0,contents.Length);
}
字符串通知状态;
使用(HttpWebResponse=(HttpWebResponse)request.GetResponse())
{
notificationStatus=response.Headers[“X-notificationStatus”];
}
返回通知状态;
}
}
您复制了带有的示例“将您的频道放在这里”
原样的值。你需要提供一个真实的频道URI,因为这个URI格式不正确,因此抛出了UriFormatException
我更改了它,并给了它一个正确的频道名称,但它仍然不起作用。你能显示你使用的代码并详细说明你得到的异常吗?没有这一点,任何帮助都只能猜测。
public string SendNotification(string message)
{
string channelURI = "PUT_YOUR_CHANNEL_URI_HERE";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(channelURI);
request.Method = "POST";
request.ContentType = "text/xml";
request.Headers.Add("X-NotificationClass", "2");
request.Headers.Add("X-WindowsPhone-Target", "toast");
string notificationData = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<wp:Notification xmlns:wp=\"WPNotification\">" +
"<wp:Toast>" +
"<wp:Text1>WP7 TOAST</wp:Text1>" +
"<wp:Text2>" + message + "</wp:Text2>" +
"</wp:Toast>" +
"</wp:Notification>";
byte[] contents = Encoding.Default.GetBytes(notificationData);
request.ContentLength = contents.Length;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(contents, 0, contents.Length);
}
string notificationStatus;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
notificationStatus = response.Headers["X-NotificationStatus"];
}
return notificationStatus;
}
}