從URL如「http://localhost:2111/
」如何將端口部分的地址部分:http://localhost/
分開:2111?是否有數據結構允許我將http://localhost:2111/
分開或構建到其地址和端口?獲取不帶端口.net的地址,並將地址+端口構造到端點
回答
使用此:
Uri uri = new Uri("http://localhost:2111/");
string newUri = uri.Scheme + "://" + uri.Host + "/";
Console.WriteLine(newUri);
// Output:
// http://localhost/
要反其道而行之:
Uri uri = new Uri("http://localhost/");
string newURI = uri.AbsoluteUri + uri.Port;
對我來說uri.Ports
回報80
,我不知道它是否適合你,但給它一個嘗試。
如何做到相反? – 2013-04-09 08:00:13
「對面」是什麼意思? – Andy 2013-04-09 08:00:54
從'http:// localhost /'構建'http:// localhost:2111 /'和2111 – 2013-04-09 08:01:44
UriBuilder可用於通過其端口值設置爲-1或80以除去從URL中的端口:
var uriBuilder = new UriBuilder("http://localhost:2111/");
uriBuilder.Port = -1; // or 80
string newUrl = uriBuilder.Uri.AbsoluteUri;
Console.WriteLine(newUrl);
上面將輸出http://localhost/
。
如果你想將它們添加端口一起回來,然後再使用UriBuilder,並設置爲2111:
var uriBuilder = new UriBuilder("http://localhost/");
uriBuilder.Port = 2111;
string newUrl = uriBuilder.Uri.AbsoluteUri;
Console.WriteLine(newUrl);
上面會輸出http://localhost/2111
。
- 1. 獲取IP地址和UDP端口包
- 2. 獲取對等IP地址和端口
- 3. SMTP地址和端口
- 4. 獲取C端口客戶端的IP地址
- 5. CFStreamCreatePairWithSocketToHost獲取本地端點地址
- 6. 獲取客戶端IP地址和端口?
- 7. 如何在php中獲取客戶端ip地址和端口?
- 8. CORBA ::使用IIOP獲取客戶端ORB地址和端口
- 9. 獲取客戶端IP地址:端口(System.Runtime.Remoting.Channels)
- 10. 從Swift的sockaddr_in結構中獲取IP地址和端口號?
- 11. dns到directadmin ip地址:端口
- 12. WCF端點地址
- 13. server.address()。端口正在使用nodejs中的隨機端口地址
- 14. SNMP獲取連接到交換機端口的mac地址
- 15. Azure VM端點:將公共端口映射到不同的本地端口
- 16. 端口443(HTTP)的目標IP地址
- 17. CouchDB的IP地址和端口
- 18. 聽一個*地址的端口11111
- 19. 端口和IP地址的關係
- 20. 將標題添加到端點地址
- 21. 如何通過控制端口獲取Tor出口節點的IP地址?
- 22. WCF端點的基地址
- 23. 排序IPv4地址與端口號
- 24. 爲JGroups配置地址和端口
- 25. openGTS IP和端口地址設置
- 26. 異或IP地址和端口號
- 27. 使端口和地址可配置
- 28. constexpr引用avr端口地址
- 29. IP地址和端口號C#
- 30. 地址端口擴展器PCF8574
看看'Uri'類。 – leppie 2013-04-09 07:53:00
@leppie似乎我不能從'http:// localhost /'和2111 – 2013-04-09 07:57:35
構建'http:// localhost:2111 /'爲什麼你不能? – leppie 2013-04-09 08:00:49