开发者

Invalid URI: The hostname could not be parsed

I am trying to Construct a URI. But I am unable to handle bad URIs.

Is there any way we can 开发者_高级运维handle bad URIs?

Code I am using:

if (reviews[e.Item.ItemIndex].URL.ToString().Contains("http:"))
{
      oURI = new Uri(reviews[e.Item.ItemIndex].URL.ToString());
}
else 
{
   oURI = new Uri("http://"+ reviews[e.Item.ItemIndex].URL.ToString());
}

else part gets error out for bad URIs.

Thank you!


Call Uri.TryCreate:

string original = reviews[e.Item.ItemIndex].URL.ToString();
if (!original.StartsWith("http:"))
    original = "http://" + original;
Uri uri;
if (!Uri.TryCreate(original, UriKind.Absolute, out uri)) {
    //Bad bad bad!
}


I had a space after "http:// " like http:// exampleServer/exampleservice.svc which had caused this error.


For anyone, this could be the issue, There was Invalid URI: The hostname could not be parsed for me on some urls and one some urls it was not coming.

I looked into the urls making issue, they had '\' instead of '/' in urls,

so in URI parsing it throws error.


Maybe this will help some poor soul in the future. I just spent 5 hours bashing my head against a wall trying to figure out why SpecFlow was choking on this exact error. I couldn't find the smoking gun, but I am fairly certain for me it was the path name was too long, or potentially there was something bad in the path. When I moved the solution to smaller path name it worked. Here is the SO question/answer I posted about the specifics around the error I was getting.


I got similar error with nginx fastcgi-mono-server4. It turns out that webserver provided regex value for SERVER_NAME. So I have to fix it on the server by replacing fastcgi_param SERVER_NAME $server_name; with fastcgi_param SERVER_NAME $host;


I got into a similar error when trying to construct a URI object in C#. For me the problem was the length.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜