问题描述
有没有在.NET中可用,让我建立一个URL的辅助类?
Are there any helper classes available in .NET to allow me to build a Url?
例如,如果用户输入的字符串
For example, if a user enters a string:
stackoverflow.com
和我试图传递给的HttpWebRequest
:
WebRequest.CreateHttp(url);
这将失败,因为它不是一个有效的URL(它没有preFIX)。
It will fail, because it is not a valid url (it has no prefix).
我要的是能够分析该部分URL用户输入:
What i want is to be able to parse the partial url the user entered:
Uri uri = new Uri(url);
,然后修复缺失的部分:
and then fix the missing pieces:
if (uri.Port == 0)
uri.Port = 3333;
if (uri.Scheme == "")
uri.Scheme = "https";
是否.NET具有可用于解析和操作的任何类的URI?
Does .NET have any classes that can be used to parse and manipulate Uri's?
这是用户输入的值(例如: stackoverflow.com:3333
)是有效的;我只是需要一个类来接它拆开。我的尝试的使用 UriBuilder
类:
The value that the user entered (e.g. stackoverflow.com:3333
) is valid; i just need a class to pick it apart. i tried using the UriBuilder
class:
UriBuilder uriBuilder = new UriBuilder("stackoverflow.com:3333");
不幸的是,UriBuilder类是无法处理的URI:
unfortunately, the UriBuilder class is unable to handle URIs:
- uriBuilder.Path =
3333
- uriBuilder.Port =
1
- uriBuidler.Scheme =
stackoverflow.com
- uriBuilder.Path =
3333
- uriBuilder.Port =
-1
- uriBuidler.Scheme =
stackoverflow.com
所以我需要一个可以理解的类主持人:端口
,其中特别是变得很重要时的的。
So i need a class that can understand host:port
, which especially becomes important when it's not particularly http
, but could be.
控制台应用程序。
网址的需要解析的一些例子:
Some examples of URL's that require parsing:
-
服务器:8088
-
服务器:8088 / FUNC1
-
服务器:8088 / FUNC1 / SubFunc1
-
的http://服务器
-
的http://服务器/ FUNC1
-
的http://服务器/ FUNC / SubFunc1
-
的http://服务器:8088
-
的http://服务器:8088 / FUNC1
-
的http://服务器:8088 / FUNC1 / SubFunc1
-
磁铁://服务器
-
磁铁://服务器/ FUNC1
-
磁铁://服务器/ FUNC / SubFunc1
-
磁铁://服务器:8088
-
磁铁://服务器:8088 / FUNC1
-
磁铁://服务器:8088 / FUNC1 / SubFunc1
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
URL的格式为:
foo://example.com:8042/over/there?name=ferret#nose
\_/ \_________/ \__/\_________/\__________/ \__/
| | | | | |
scheme host port path query fragment
另请参见
- LMBTFY
- LMBTFY
See also
推荐答案
如果您需要确保一些字符串来了作为用户输入有效的网址,你可以使用<$c$c>Uri.TryCreate$c$c>方法:
If you need to ensure that some string coming as user input is valid url you could use the Uri.TryCreate
method:
Uri uri;
string someUrl = ...
if (!Uri.TryCreate(someUrl, UriKiind.Absolute, out uri))
{
// the someUrl string did not contain a valid url => inform your users about that
}
else
{
var request = WbeRequest.Create(uri);
... safely proceed with executing the request
}
现在,如果在另一方面,你不想被建立的网址在.NET还有的<$c$c>UriBuilder$c$c>类专门为此目的而设计的。让我们来举个例子。假设你想建立以下网址: http://example.com/path?foo=bar&baz=bazinga#some_fragment
其中酒吧
和 bazinga
值是来自用户的:
Now if on the other hand you want to be building urls in .NET there's the UriBuilder
class specifically designed for that purpose. Let's take an example. Suppose you wanted to build the following url: http://example.com/path?foo=bar&baz=bazinga#some_fragment
where the bar
and bazinga
values are coming from the user:
string foo = ... coming from user input
string baz = ... coming from user input
var uriBuilder = new UriBuilder("http://example.com/path");
var parameters = HttpUtility.ParseQueryString(string.Empty);
parameters["foo"] = foo;
parameters["baz"] = baz;
uriBuilder.Query = parameters.ToString();
uriBuilder.Fragment = "some_fragment";
Uri finalUrl = uriBuilder.Uri;
var request = WbeRequest.Create(finalUrl);
... safely proceed with executing the request
这篇关于.NET:如何建立一个网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!