System.Uri 连接到网站时引发异常

System.Uri raise an exception when connect to a website

此代码在我执行时引发 UriFormatException:

try
{
    var _client = new HttpClient();
    var s = await _client.GetStringAsync(new Uri("http://emojiapp.96.lt/"));
    EmojiList = new ObservableCollection<Emoji>
          (FetchLinksFromSource(s).Select(uri => new Emoji()
          {
               Image = new BitmapImage(uri)
          }));
}
catch
{
    throw;
}

我尝试过的所有网址都会引发相同的异常。我 运行 在一个 Uwp 项目中使用最新的 .Net 核心 nuget 包在 VS2015 下使用此代码。这是详细的堆栈跟踪:

L'exception System.UriFormatException n'a pas été gérée par le code utilisateur

HResult=-2146233033
Message=Invalid URI: The format of the URI could not be determined.
Source=System.Private.Uri
StackTrace:
     at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind)
     at System.Uri..ctor(String uriString)
     at Emoji.MainPage.FetchLinksFromSource(String htmlSource)
     at Emoji.MainPage.<OnNavigatedTo>d__6.MoveNext()
InnerException: 

你有能力 "internetClient" 在清单文件中启用吗?

在我的 UWP 应用程序中,你的代码运行良好,也许你有阻止请求的防火墙?

例外情况很明显:at Emoji.MainPage.FetchLinksFromSource(String htmlSource),所以问题与 new Uri("http://emojiapp.96.lt/") 你得到的回复表无关,但与 FetchLinksFromSource(s).Select(uri => new Emoji(){Image = new BitmapImage(uri)}).

有关

FetchLinksFromSource function get only links that refers to images in the page

我不知道你是如何编写代码的,这是我使用 HtmlAgilityPack 解析 HTML 内容并获取每个图像的 uri 的演示:

try
{
    var _client = new HttpClient();
    var html = await _client.GetStringAsync(new Uri("http://emojiapp.96.lt/"));
    HtmlDocument doc = new HtmlDocument();
    doc.LoadHtml(html);
    var imgs = doc.DocumentNode.Descendants("img"); // Give you all the img
    foreach (var img in imgs)
    {
        var imgUristring = img.Attributes.FirstOrDefault().Value;
    }
}
catch
{
    throw;
}

所以本网站中图像的解析 uri 是例如这样的:"images/2531771462985282.jpg",我确定这个 uri 不是可以在 UWP 中用于 HttpClient 的有效 uri或 BitmapImage,这更像是该网站的内部图像地址。