C# Screen Scraper - 处理长 uri

发布于 2024-08-02 10:59:44 字数 282 浏览 2 评论 0原文

我正在构建一个 html 屏幕抓取工具,它解析 url,然后将它们与一组其他 url 进行比较。

比较是通过 Uri.AbsoluteUri 或 Uri.Host 完成的。

我的问题是,当我创建一个新的 Uri (new Uri(url)) 时,当 url 太长或包含很多斜杠时,会引发 UriFormatException。

由于我的预定义 url 集包含几个(到)长 url,因此我不能仅使用子字符串来仅获取 url 的一部分。

处理这个问题的最佳方法是什么?

谢谢

I'm building a html screen scraper, which parses urls, and then compare those with a set of other urls.

The comparison is done with Uri.AbsoluteUri or Uri.Host.

My problem is that when i'm creating a new Uri (new Uri(url)), an UriFormatException is thrown when the url is to long, or contains to many slashes.

Since my predefined set of urls contains several (to) long urls, I cannot just use substring to only fetch a part of the url.

What would be the best way to handle this?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

决绝 2024-08-09 10:59:44

您可以使用 Uri.TryCreate 检查是否URI 在您新建之前是有效的。

您不应该在这么短的 url 上遇到异常。以下程序在VS2008上运行良好:

static void Main(string[] args)
{
    Uri uri = new Uri("http://stackoverflow.com/questions/1298985/c-screen-scraper-handle-long-uris/c-screen-scraper-handle-long-uris/c-screen-scraper-handle-long-uris/c-screen-scraper-handle-long-uris/c-screen-scraper-handle-long-uris/c-screen-scraper-handle-long-uris/c-screen-scraper-handle-long-uris/c-screen-scraper-handle-long-uris/");
    Uri uri2 = new Uri("http://stackoverflow.com/questions/1298985/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/");
    Console.ReadLine();
}

You can use Uri.TryCreate to check if the URI is valid before you new it.

You should not get an exception on a url this is so short. The folowing program runs well on VS2008:

static void Main(string[] args)
{
    Uri uri = new Uri("http://stackoverflow.com/questions/1298985/c-screen-scraper-handle-long-uris/c-screen-scraper-handle-long-uris/c-screen-scraper-handle-long-uris/c-screen-scraper-handle-long-uris/c-screen-scraper-handle-long-uris/c-screen-scraper-handle-long-uris/c-screen-scraper-handle-long-uris/c-screen-scraper-handle-long-uris/");
    Uri uri2 = new Uri("http://stackoverflow.com/questions/1298985/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/");
    Console.ReadLine();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文