getHost()函數是URI類的一部分。函數getHost()返回指定URI的主機。
URL的主機部分是URI的主機名。
函數簽名:
public String getHost()
返回類型函數返回字符串類型
用法
url.getHost()
參數:此函數不需要任何參數
以下程序將說明getHost()函數的用法
範例1:
// Java program to show the
// use of the function getHost()
import java.net.*;
class Solution {
public static void main(String args[])
{
// URI object
URI uri = null;
try {
// create a URI
uri = new URI("https://www.geeksforgeeks.org");
// get the Host
String host = uri.getHost();
// display the URI
System.out.println("URI = " + uri);
// display the Host
System.out.println("Host = " + host);
}
// if any error occurs
catch (URISyntaxException e) {
// display the error
System.out.println("URI Exception =" + e.getMessage());
}
}
}
輸出:
URI = https://www.geeksforgeeks.org Host = www.geeksforgeeks.org
範例2:getAuthority()和getHost()函數之間的區別在於,getAuthority()隨端口一起返回主機,而getHost()僅返回主機名。
// Java program to show the
// use of the function getHost()
import java.net.*;
class Solution {
public static void main(String args[])
{
// url object
URI uri = null;
try {
// create a URI
uri = new URI("https://www.geeksforgeeks.org:80");
// get the Authority
String authority = uri.getAuthority();
// get the Host
String host = uri.getHost();
// display the URI
System.out.println("URI = " + uri);
// display the Authority
System.out.println("Authority = " + authority);
// display the Host
System.out.println("Host = " + host);
}
// if any error occurs
catch (URISyntaxException e) {
// display the error
System.out.println("URI Exception =" + e.getMessage());
}
}
}
輸出:
URI = https://www.geeksforgeeks.org:80 Authority = www.geeksforgeeks.org:80 Host = www.geeksforgeeks.org
相關用法
- Java URL getHost()用法及代碼示例
- Java Java lang.Long.byteValue()用法及代碼示例
- Java Java lang.Long.highestOneBit()用法及代碼示例
- Java Java lang.Long.numberOfLeadingZeros()用法及代碼示例
- Java Java.util.Collections.disjoint()用法及代碼示例
- Java Java lang.Long.builtcount()用法及代碼示例
- Java Java lang.Long.reverse()用法及代碼示例
- Java Java lang.Long.lowestOneBit()用法及代碼示例
- Java Java.util.Collections.rotate()用法及代碼示例
- Java Java lang.Long.numberOfTrailingZeros()用法及代碼示例
- Java Clock withZone()用法及代碼示例
- Java Clock tickMinutes()用法及代碼示例
- Java Set contains()用法及代碼示例
注:本文由純淨天空篩選整理自andrew1234大神的英文原創作品 URI getHost() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。