无效 URI:无法解析主机名
我正在尝试构建一个 URI。但我无法处理错误的 URI。
我们有什么办法可以处理错误的 URI 吗?
我正在使用的代码:
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
部分因错误 URI 而出错。
谢谢你!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
调用
Uri.TryCreate
:Call
Uri.TryCreate
:我在“http://”之后有一个空格,例如 http:// exampleServer/exampleservice.svc ,这导致了此错误。
I had a space after "http:// " like http:// exampleServer/exampleservice.svc which had caused this error.
对于任何人来说,这可能是一个问题,
在某些网址上出现
无效的 URI:无法解析主机名
,而在某些网址上则无法解析。我研究了 url 制作问题,
他们在 url 中使用了“\”而不是“/”
,因此在 URI 解析中它会抛出错误。
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.
也许这会在未来帮助一些可怜的灵魂。我花了 5 个小时把头撞在墙上,试图弄清楚为什么 SpecFlow 会因为这个错误而窒息。我找不到确凿的证据,但我相当确定是路径名太长,或者路径中可能存在问题。当我将解决方案移至较小的路径名时,它起作用了。这是我发布的关于我遇到的错误的具体细节的SO问题/答案。
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.
我在 nginx fastcgi-mono-server4 上遇到了类似的错误。事实证明,网络服务器为 SERVER_NAME 提供了正则表达式值。因此,我必须在服务器上修复它,方法是将
fastcgi_param SERVER_NAME $server_name;
替换为fastcgi_param SERVER_NAME $host;
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;
withfastcgi_param SERVER_NAME $host;
当尝试在 C# 中构造 URI 对象时,我遇到了类似的错误。
对我来说,问题是长度。
I got into a similar error when trying to construct a URI object in C#.
For me the problem was the length.