类型' System.UriFormatException'的例外情况发生在System.dll中但未在用户代码中处理

时间:2015-07-30 21:15:26

标签: c# httpwebrequest uri

每次运行应用程序时都会出现此错误,生成uri但是当我尝试通过单击aspn网页上的按钮发送Toast通知时程序崩溃并显示该错误(类型&#的例外) 39; System.UriFormatException'发生在System.dll但未在用户代码中处理)这是我的代码线程:[崩溃在HttpWebRequest请求=(HttpWebRequest)WebRequest.Create(channelURI); line]

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

1 个答案:

答案 0 :(得分:0)

您原样复制了"PUT_YOUR_CHANNEL_URI_HERE"值的样本。您需要提供一个真实的渠道URI,因为这个URI格式不正确,因此会抛出UriFormatException