System.UriFormatException:无效的 URI:无法解析主机名

Posted

技术标签:

【中文标题】System.UriFormatException:无效的 URI:无法解析主机名【英文标题】:System.UriFormatException: Invalid URI: The hostname could not be parsed 【发布时间】:2011-02-18 09:17:38 【问题描述】:

我的网站突然出现以下错误。它不访问数据库。这只是一个使用 .NET 2.0 的简单网站。

我最近确实应用了可用的 Windows Server 2003 服务包。那会改变事情吗?

我应该补充一点,错误随机出现和消失,并且今天和昨天一直如此。我放了5分钟,错误就消失了。

“/”应用程序中的服务器错误。

无效的 URI:无法解析主机名。说明:一个 当前 web 执行过程中发生未处理的异常 要求。请查看堆栈跟踪以获取有关 错误及其源自代码的位置。

异常详情:

System.UriFormatException: 无效的 URI: 主机名不能 已解析。

来源错误:

在执行过程中产生了一个未处理的异常 当前的网络请求。有关原产地和位置的信息 可以使用下面的异常堆栈跟踪来识别异常。

堆栈跟踪:

[UriFormatException:无效的 URI:无法解析主机名。] System.Uri.CreateThis(字符串 uri,布尔值 dontEscape,UriKind uriKind) +5367536 System.Uri.CreateUri(Uri baseUri, String relativeUri, Boolean dontEscape) +31 System.Uri..ctor(Uri baseUri, String relativeUri) +34 System.Net.HttpWebRequest.CheckResubmit(Exception& e) +5300867

[WebException: 无法处理从 HTTP/HTTPS 协议重定向到 其他不同的。] System.Net.HttpWebRequest.GetResponse() +5314029 System.Xml.XmlDownloadManager.GetNonFileStream(Uri uri,ICredentials 凭据)+69 System.Xml.XmlDownloadManager.GetStream(Uri uri,ICredentials 凭据)+3929371 System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String 角色, Type ofObjectToReturn) +54 System.Xml.XmlTextReaderImpl.OpenUrlDelegate(对象 xmlResolver) +74 System.Threading.CompressedStack.runTryCode(Object userData) +70 System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode 代码,CleanupCode backoutCode,对象 userData) +0 System.Threading.CompressedStack.Run(CompressedStack 压缩栈, ContextCallback 回调,对象状态)+108 System.Xml.XmlTextReaderImpl.OpenUrl() +186 System.Xml.XmlTextReaderImpl.Read() +208 System.Xml.XmlLoader.Load(XmlDocument 文档,XmlReader 阅读器,布尔值 保留空白)+112 System.Xml.XmlDocument.Load(XmlReader 读者)+108 System.Web.UI.WebControls.XmlDataSource.PopulateXmlDocument(XmlDocument 文档、CacheDependency& 数据CacheDependency、CacheDependency& transformCacheDependency) +303 System.Web.UI.WebControls.XmlDataSource.GetXmlDocument() +153 System.Web.UI.WebControls.XmlDataSourceView.ExecuteSelect(DataSourceSelectArguments 参数)+29 System.Web.UI.WebControls.BaseDataList.GetData() +39 System.Web.UI.WebControls.DataList.CreateControlHierarchy(布尔 使用数据源)+264 System.Web.UI.WebControls.BaseDataList.OnDataBinding(EventArgs e) +55 System.Web.UI.WebControls.BaseDataList.DataBind() +75 System.Web.UI.WebControls.BaseDataList.EnsureDataBound() +55 System.Web.UI.WebControls.BaseDataList.CreateChildControls() +65 System.Web.UI.Control.EnsureChildControls() +97 System.Web.UI.Control.PreRenderRecursiveInternal() +53 System.Web.UI.Control.PreRenderRecursiveInternal() +202 System.Web.UI.Control.PreRenderRecursiveInternal() +202 System.Web.UI.Control.PreRenderRecursiveInternal() +202 System.Web.UI.Control.PreRenderRecursiveInternal() +202 System.Web.UI.Page.ProcessRequestMain(布尔值 includeStagesBeforeAsyncPoint,布尔型 includeStagesAfterAsyncPoint) +4588

【问题讨论】:

请告诉我们您的XmlDataSource hmm.. 站点中唯一的 xml 位于用于 RadRotator (telerik) 的 xml 文件中。但这多少年来都没有改变。唯一的另一件事是 web.config 文件。 此堆栈跟踪来自绑定到 XmlDataSource 的 DataList。 清除 radrotator 错误,因为该错误也出现在没有它的页面上。 啊,你说得对。这就是它所说的。问题是没有数据列表,除非它是 Telerik 控件的一部分。我看不到任何可以缩小错误起源的地方。可以吗? 【参考方案1】:

Uri.Create 和 Uri.TryCreate 中存在一些错误,允许它们创建随后无法解析的无效 URI。我不时遇到这种情况,但无法追踪导致它的 url 字符串。我在here 发布了一些关于它的信息。

如果您有一个 url 列表并且知道其中一个会导致问题(我没有那么奢侈,因为我在没有保存页面文本的网络爬虫中遇到了这个问题),您可以找到类似这样的伪代码的错误:

while not end of file

    string url = read from file
    Uri uri = new Uri(url);
    try
    
        string host = uri.Host;
    
    catch (UriFormatException)
    
        Console.WriteLine("Bad url: 0", url);
    

如果您能识别出一些导致此异常的 url,我肯定希望看到它们。

【讨论】:

试试这个字符串:“mailto:foo[at]bar.com” @huseyint:字符串"foo@bar.com" 按预期工作。字符串"foo[at]bar.com" 在构造函数中引发异常,再次符合预期。我遇到的错误是使用 Uri 构造函数成功的字符串,但随后访问 Host 属性会引发异常。

以上是关于System.UriFormatException:无效的 URI:无法解析主机名的主要内容,如果未能解决你的问题,请参考以下文章

将 ASP.NET 应用程序部署到 Azure Web 应用程序时出现 HTTP 500.79 错误/System.UriFormatException

从特定文件加载图像

小心 HttpClient 中 FormUrlEncodeContent 的 bug

小心 HttpClient 中的 FormUrlEncodeContent 的 bug