无效 URI:无法解析主机名

发布于 2024-10-02 23:59:36 字数 378 浏览 19 评论 0原文

我正在尝试构建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

迷离° 2024-10-09 23:59:36

调用 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!
}

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!
}
迷荒 2024-10-09 23:59:36

我在“http://”之后有一个空格,例如 http:// exampleServer/exampleservice.svc ,这导致了此错误。

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

淡水深流 2024-10-09 23:59:36

对于任何人来说,这可能是一个问题,
在某些网址上出现无效的 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.

梦与时光遇 2024-10-09 23:59:36

也许这会在未来帮助一些可怜的灵魂。我花了 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.

国际总奸 2024-10-09 23:59:36

我在 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; with fastcgi_param SERVER_NAME $host;

挽心 2024-10-09 23:59:36

当尝试在 C# 中构造 URI 对象时,我遇到了类似的错误。
对我来说,问题是长度。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文