gpt4 book ai didi

c# - 如何在 C# 中定义 URL

转载 作者:行者123 更新时间:2023-12-05 08:24:07 24 4
gpt4 key购买 nike

我有这样的东西

WebRequest request = WebRequest.Create("http://127.0.0.1/");

我想使用类似于

的东西
#define URL @""http://127.0.0.1/"
WebRequest request = WebRequest.Create(URL);

我如何在 C# 中执行此操作?

最佳答案

我不确定我是否理解正确,但如果你想定义一个 URL,你不需要创建一个 WebRequest...

例如,您应该使用 Uri 类型:

Uri ur = new Uri("http://www.google.com");

这里是一些如何从 Uri 类型中检索数据的示例:

    Console.WriteLine("AbsolutePath = {0}", uri.AbsolutePath);
Console.WriteLine("AbsoluteUri = {0}", uri.AbsoluteUri);
Console.WriteLine("Authority = {0}", uri.Authority);
Console.WriteLine("DnsSafeHost = {0}", uri.DnsSafeHost);
Console.WriteLine("Fragment = {0}", uri.Fragment);
Console.WriteLine("Host = {0}", uri.Host);
Console.WriteLine("HostNameType = {0}", uri.HostNameType);
Console.WriteLine("IsAbsoluteUri = {0}", uri.IsAbsoluteUri);
Console.WriteLine("IsDefaultPort = {0}", uri.IsDefaultPort);
Console.WriteLine("IsFile = {0}", uri.IsFile);
Console.WriteLine("IsLoopback = {0}", uri.IsLoopback);
Console.WriteLine("IsUnc = {0}", uri.IsUnc);
Console.WriteLine("LocalPath = {0}", uri.LocalPath);
Console.WriteLine("OriginalString = {0}", uri.OriginalString);
Console.WriteLine("PathAndQuery = {0}", uri.PathAndQuery);
Console.WriteLine("Port = {0}", uri.Port);
Console.WriteLine("Query = {0}", uri.Query);
Console.WriteLine("Scheme = {0}", uri.Scheme);
Console.WriteLine("Segments = {0}", string.Join(",", uri.Segments));
Console.WriteLine("UserEscaped = {0}", uri.UserEscaped);
Console.WriteLine("UserInfo = {0}", uri.UserInfo);
Console.WriteLine(new string('-', 40));

查看更多信息 here

关于c# - 如何在 C# 中定义 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23779441/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com