如何建立一个网址? [英] How to build a Url?
问题描述
是否有任何辅助类在.NET中可用,让我建立一个网址
例如,如果用户输入的字符串:
stackoverflow.com
我试图传递给的HttpWebRequest
:
的WebRequest。 CreateHttp(URL);
这将失败,因为它不是一个有效的URL(它没有前缀)。
我要的是能够解析部分URL用户输入:
开放的URI =新的URI(URL);
,然后修复缺失的部分:
如果(uri.Port == 0)
uri.Port = 3333;
如果(uri.Scheme ==)
uri.Scheme =https开头;
请问.NET有可用于分析和操作的URI?
$任何类b$ b
的UriBuilder类不能做的工作
在用户输入的值(如计算器。玉米:3333
)是有效的;我只是需要一个类来接它拆开。我的尝试的使用 UriBuilder
类:
UriBuilder uriBuilder =新UriBuilder(stackoverflow.com:3333);
不幸的是,UriBuilder类是无法处理的URI:
- uriBuilder.Path =
3333
- uriBuilder.Port =
1
- uriBuidler.Scheme =
stackoverflow.com
所以我需要一类,可以理解主持人:端口
,其中尤其以变得很重要时的它并不特别 HTTP
,但可以
。
奖金颤振
控制台应用程序。
从另一个问题
这需要解析的URL的一些例子:
-
服务器: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
URL的格式为:
富://示例.com:8042 /过/有名字=#鼬鼻子
\_ / \ _________ / \ __ / \ _________ / \ __________ / \ __ /
| | | | | |
方案主机端口路径查询片段
如果你需要确保一些字符串来作为用户输入有效的网址,你可以使用的 Uri.TryCreate
方式:
乌里URI;
串someUrl = ...
如果(!Uri.TryCreate(someUrl,UriKind.Absolute,出URI))
{
//将someUrl字符串不包含有效网址
//告知用户有关
}
,否则
{
VAR请求= WebRequest.Create(URI);
// ...安全地执行请求
}
进行
现在如果在另一方面,你想在.NET构建URLs还有的的 UriBuilder
类专门为此目的而设计的。让我们举一个例子。假设你想建立以下网址: http://example.com/path?foo=bar&baz=bazinga#some_fragment
其中酒吧
和 bazinga
值来自用户:
串富= ...从用户输入
串巴兹= ...来从用户输入
VAR uriBuilder即将=新uriBuilder(http://example.com/路径);
VAR参数= HttpUtility.ParseQueryString(的String.Empty);
参数[富] = foo的;
参数[巴兹] =巴兹;
uriBuilder.Query = parameters.ToString();
uriBuilder.Fragment =some_fragment;
乌里finalUrl = uriBuilder.Uri;
VAR请求= WebRequest.Create(finalUrl);
...安全地执行请求
进行
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
and i try to pass that to an HttpWebRequest
:
WebRequest.CreateHttp(url);
It will fail, because it is not a valid url (it has no prefix).
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";
Does .NET have any classes that can be used to parse and manipulate Uri's?
The UriBuilder class can't do the job
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");
unfortunately, the UriBuilder class is unable to handle URIs:
- 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.
Bonus Chatter
Console application.
From the other question
Some examples of URL's that require parsing:
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
The format of a Url is:
foo://example.com:8042/over/there?name=ferret#nose
\_/ \_________/ \__/\_________/\__________/ \__/
| | | | | |
scheme host port path query fragment
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, UriKind.Absolute, out uri))
{
// the someUrl string did not contain a valid url
// inform your users about that
}
else
{
var request = WebRequest.Create(uri);
// ... safely proceed with executing the request
}
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 = WebRequest.Create(finalUrl);
... safely proceed with executing the request
这篇关于如何建立一个网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!