我正在寻找可以解析 URL 的 .NET Framework 类。
一些需要解析的 URL 示例:
server:8088
server:8088/func1
server:8088/func1/SubFunc1
http://server
http://server/func1
http://server/func/SubFunc1
http://server:8088
http://server:8088/func1
http://server:8088/func1/SubFunc1
magnet://server
magnet://server/func1
magnet://server/func/SubFunc1
magnet://server:8088
magnet://server:8088/func1
magnet://server:8088/func1/SubFunc1
问题是 Uri
和 UriBuilder
类没有正确处理 URL。例如,他们对以下内容感到困惑:
stackoverflow.com:8088
网址背景
Url 的格式是:
foo://example.com:8042/over/there?name=ferret#nose
\_/ \_________/ \__/\_________/\__________/ \__/
| | | | | |
scheme host port path query fragment
在我们的例子中,我们只关心:
Uri.Scheme
Uri.Host
Uri.Port
Uri.Path
测试
运行一些测试,我们可以检查 UriBuilder
类如何处理各种 Uri:
Expected Expected Expected Expected
//Test URI Scheme Server Port Path
//===================================== ======== ======== ==== ====================
t("server", "", "server", -1, "");
t("server/func1", "", "server", -1, "/func1");
t("server/func1/SubFunc1", "", "server", -1, "/func1/SubFunc1");
t("server:8088", "", "server", 8088, "");
t("server:8088/func1", "", "server", 8088, "/func1");
t("server:8088/func1/SubFunc1", "", "server", 8088, "/func1/SubFunc1");
t("http://server", "http", "server", -1, "/func1");
t("http://server/func1", "http", "server", -1, "/func1");
t("http://server/func/SubFunc1", "http", "server", -1, "/func1/SubFunc1");
t("http://server:8088", "http", "server", 8088, "");
t("http://server:8088/func1", "http", "server", 8088, "/func1");
t("http://server:8088/func1/SubFunc1", "http", "server", 8088, "/func1/SubFunc1");
t("magnet://server", "magnet", "server", -1, "");
t("magnet://server/func1", "magnet", "server", -1, "/func1");
t("magnet://server/func/SubFunc1", "magnet", "server", -1, "/func/SubFunc1");
t("magnet://server:8088", "magnet", "server", 8088, "");
t("magnet://server:8088/func1", "magnet", "server", 8088, "/func1");
t("magnet://server:8088/func1/SubFunc1", "magnet", "server", 8088, "/func1/SubFunc1");
除六个案异常(exception),所有案例都无法正确解析:
Url Scheme Host Port Path
=================================== ====== ====== ==== ===============
server http server 80 /
server/func1 http server 80 /func1
server/func1/SubFunc1 http server 80 /func1/SubFunc1
server:8088 server -1 8088
server:8088/func1 server -1 8088/func1
server:8088/func1/SubFunc1 server -1 8088/func1/SubFunc1
http://server http server 80 /
http://server/func1 http server 80 /func1
http://server/func/SubFunc1 http server 80 /func1/SubFunc1
http://server:8088 http server 8088 /
http://server:8088/func1 http server 8088 /func1
http://server:8088/func1/SubFunc1 http server 8088 /func1/SubFunc1
magnet://server magnet server -1 /
magnet://server/func1 magnet server -1 /func1
magnet://server/func/SubFunc1 magnet server -1 /func/SubFunc1
magnet://server:8088 magnet server 8088 /
magnet://server:8088/func1 magnet server 8088 /func1
magnet://server:8088/func1/SubFunc1 magnet server 8088 /func1/SubFunc1
我说我想要一个 .NET Framework 类。我也愿意接受任何我可以捡起来咀嚼的口香糖。只要它满足我简单的测试用例。
奖金聊天
我正在考虑扩展 this question ,但该问题仅限于 http
。
我也问过这个same question earlier today , 但我现在意识到我措辞不正确。我错误地询问了如何“构建” 一个 url。实际上,我想“解析” 用户输入的 URL。我现在不能回去从根本上改变标题。所以我会再次问同样的问题,只是更好,更明确的目标,在这里。
红利阅读
最佳答案
这个正则表达式行吗?
^((?<schema>[a-z]*)://)?(?<host>[^/:]*)?(:(?<port>[0-9]*))?(?<path>/.*)?$
它并不完美,但它似乎适用于您的测试用例。
关于c# - 在 .NET 中解析 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20169991/