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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With