【问题标题】:An exception of type 'System.UriFormatException' occurred in System.dll but was not handled in user codeSystem.dll 中出现“System.UriFormatException”类型的异常,但未在用户代码中处理
【发布时间】:2015-07-30 21:15:26
【问题描述】:

我每次运行应用程序时都会收到此错误,生成 uri 但是当我尝试通过单击 aspn 网页上的按钮发送 toast 通知时,程序崩溃并显示该错误(“系统”类型的异常.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;
        }
    }

【问题讨论】:

    标签: c# httpwebrequest uri


    【解决方案1】:

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

    【讨论】:

    • 我改变了它并给了它一个正确的频道名称,但它仍然无法正常工作
    • 您能否展示您使用的代码并详细说明您遇到的异常?没有这个,任何帮助都将被猜测出来。
    猜你喜欢
    • 1970-01-01
    • 2011-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-31
    • 2016-02-13
    相关资源
    最近更新 更多