System.UriFormatException: Invalid URI: The hostname could not be parsed
Asked Answered
H

1

10

All of a sudden I'm getting the following error on my website. It doesn't access a database. It's just a simple website using .NET 2.0.

I did recently apply the available windows server 2003 service packs. Could that have changed things?

I should add that the error randomly comes and goes and has been doing so for today and yesterday. I leave it for 5 minutes and the error is gone.

Server Error in '/' Application.

Invalid URI: The hostname could not be parsed. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details:

System.UriFormatException: Invalid URI: The hostname could not be parsed.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[UriFormatException: Invalid URI: The hostname could not be parsed.]
System.Uri.CreateThis(String uri, Boolean 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: Cannot handle redirect from HTTP/HTTPS protocols to other dissimilar ones.] System.Net.HttpWebRequest.GetResponse() +5314029 System.Xml.XmlDownloadManager.GetNonFileStream(Uri uri, ICredentials credentials) +69
System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials) +3929371 System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn) +54
System.Xml.XmlTextReaderImpl.OpenUrlDelegate(Object xmlResolver) +74
System.Threading.CompressedStack.runTryCode(Object userData) +70
System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData) +0
System.Threading.CompressedStack.Run(CompressedStack compressedStack, ContextCallback callback, Object state) +108
System.Xml.XmlTextReaderImpl.OpenUrl() +186
System.Xml.XmlTextReaderImpl.Read() +208
System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace) +112 System.Xml.XmlDocument.Load(XmlReader reader) +108
System.Web.UI.WebControls.XmlDataSource.PopulateXmlDocument(XmlDocument document, CacheDependency& dataCacheDependency, CacheDependency& transformCacheDependency) +303
System.Web.UI.WebControls.XmlDataSource.GetXmlDocument() +153
System.Web.UI.WebControls.XmlDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) +29 System.Web.UI.WebControls.BaseDataList.GetData() +39 System.Web.UI.WebControls.DataList.CreateControlHierarchy(Boolean useDataSource) +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(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4588

Hong answered 11/5, 2010 at 22:44 Comment(9)
Please show us your XmlDataSource.Crownwork
hmm.. the only xml in the site is in an xml file used for a RadRotator (telerik). But that's not been changed in ages. The only other thing is the web.config file.Hong
This stack trace is from a DataList bound to an XmlDataSource.Crownwork
Scratch the radrotator error, since the error showed up on pages that didnt have it also.Hong
ah, you're right. that is what it says. Trouble is there are no datalists unless it's part of a telerik control. I cant see anything to narrow down where exactly the error originiated. can you?Hong
Scratch that. there is a datalist that I use to display an rss feed. It hasnt changed in eons. I'll dig down that path.Hong
<asp:DataList ID="RssOutput" runat="server" DataSourceID="RssData" OnItemDataBound="RssOutput_ItemDataBound" CellPadding="0"> <ItemTemplate> <asp:HyperLink ID="lnkTitle" runat="server" NavigateUrl='<%# XPath("link") %>' Text='<%# XPath("title") %>'></asp:HyperLink> <asp:Label ID="lblPubDate" runat="server">(PubDate)</asp:Label><br /> <asp:Label ID="lblDescription" runat="server" Text='<%# XPath("description") %>'></asp:Label><br /> </ItemTemplate> </asp:DataList>Hong
ill surround it in a try/catch and watch the errors. obviously should have had some error handling already inthere. thanks a lot for pointing me in the right direction!!!Hong
Please show us the RssData source.Crownwork
C
6

There are some bugs in Uri.Create and Uri.TryCreate that allow them to create invalid URIs that cannot subsequently be parsed. I've run into this from time to time, but have been unable to track down the url strings that cause it. I posted a little bit about it here.

If you have a list of urls and know that one of them causes the problem (I didn't have that luxury, as I encountered this in a Web crawl where I wasn't saving the page text), you can find the error with something like this pseudocode:

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

If you can identify some urls that cause this exception, I'd sure like to see them.

Cassaundracassava answered 11/5, 2010 at 23:31 Comment(2)
Try this string: "mailto:foo[at]bar.com"Badman
@huseyint: The strings "[email protected]" works as expected. The string "foo[at]bar.com" throws an exception in the constructor, again as expected. The error I encountered was with strings for which the Uri constructor succeeds, but subsequent access of the Host property throws an exception.Cassaundracassava

© 2022 - 2024 — McMap. All rights reserved.