Java URI getRawAuthority()方法及实例
getRawAuthority()函数是URI类的一部分。getRawAuthority()函数返回一个指定URI的原始权限。这个函数返回主机名和post的准确值,如果有的话,不需要对转义的八进制序列进行解码。
函数签名:
public String getRawAuthority()
语法:
url.getRawAuthority()
参数 此函数不需要任何参数
返回类型 该函数返回字符串类型,即主机名的准确值。
下面的程序说明了getRawAuthority()函数的使用。
例1:
// Java program to show the
// use of the function getRawAuthority()
import java.net.*;
class GFG {
public static void main(String args[])
{
// URI object
URI uri = null;
try {
// create a URI
uri = new URI(
"https://www.geeksforgeeks.org");
// get the raw Authority
String raw_authority
= uri.getRawAuthority();
// display the URI
System.out.println("URI = " + uri);
// display the Raw Authority
System.out.println("Raw Authority = "
+ raw_authority);
}
// if any error occurs
catch (Exception e) {
// display the error
System.out.println(e);
}
}
}
输出:
URI = https://www.geeksforgeeks.org
Raw Authority = www.geeksforgeeks.org
例2: getRawAuthority()和getHost()函数的区别在于,getRawAuthority()返回主机和端口,而getHost()只返回主机名。
// Java program to show the
// use of the function getRawAuthority()
import java.net.*;
class GFG {
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 Raw Authority
String authority
= uri.getRawAuthority();
// get the Host
String host = uri.getHost();
// display the URI
System.out.println("URI = " + uri);
// display the raw Authority
System.out.println("Raw Authority = "
+ authority);
// display the Host
System.out.println("Host = " + host);
}
// if any error occurs
catch (Exception e) {
// display the error
System.out.println(e);
}
}
}
输出:
URI = https://www.geeksforgeeks.org:80
Raw Authority = www.geeksforgeeks.org:80
Host = www.geeksforgeeks.org
例3: getAuthority()和getRawAuthority()返回的值是一样的,只是所有转义的八位数序列都被解码了。getRawAuthority()函数返回用户提供的字符串的准确值,但getAuthority()函数对转义八位字节的序列进行解码,如果有的话。
// Java program to show the
// use of the function getAuthority()
import java.net.*;
class GFG {
public static void main(String args[])
{
// url object
URI uri = null;
try {
// create a URI
uri
= new URI(
"https://www.geeksforgeeks%E2%82%AC.org:80");
// get the Authority
String authority = uri.getAuthority();
// get the Raw Authority
String Raw_authority
= uri.getRawAuthority();
// display the URI
System.out.println("URI = " + uri);
// display the Authority
System.out.println("Authority = "
+ authority);
// display the Raw Authority
System.out.println("Raw Authority = "
+ Raw_authority);
}
// if any error occurs
catch (Exception e) {
// display the error
System.out.println(e);
}
}
}
输出:
URI = https://www.geeksforgeeks%E2%82%AC.org:80
Authority = www.geeksforgeeks?.org:80
Raw Authority = www.geeksforgeeks%E2%82%AC.org:80