Java URI getUserInfo()方法及示例
getUserInfo()函数是URI类的一部分。函数getUserInfo()返回指定URI的UserInfo部分。
函数签名:
public String getUserInfo()
语法:
uri.getUserInfo()
参数: 该函数不需要任何参数
返回类型: 该函数返回字符串类型
下面的程序说明了getUserInfo()函数的使用。
例1: 给定一个URI,我们将使用getUserInfo()函数得到UserInfo。
// Java program to show the // use of the function getUserInfo() import java.net.*; class Solution { public static void main(String args[]) { // uri object URI uri = null; try { // create a URI uri = new URI("https://Arnab_Kundu@www.geeksforgeeks.org"); // get the UserInfo String _UserInfo = uri.getUserInfo(); // display the URI System.out.println("URI = " + uri); // display the UserInfo System.out.println(" UserInfo=" + _UserInfo); } // if any error occurs catch (URISyntaxException e) { // display the error System.out.println("URL Exception =" + e.getMessage()); } }}
输出:
URI = https://Arnab_Kundu@www.geeksforgeeks.org UserInfo=Arnab_Kundu
例2: getUserInfo()和getRawUserInfo()返回的值是一样的,除了所有转义的八位数序列被解码。getRawUserInfo()返回用户提供的字符串的准确值,但是getUserInfo()函数对转义八位字节的序列(如果有)进行解码。
// Java program to show the // use of the function getUserInfo() import java.net.*; class Solution { public static void main(String args[]) { // uri object URI uri = null; try { // create a URI uri = new URI("https://Arnab_Kundu%E2%82%AC@www.geeksforgeeks.org"); // get the UserInfo String _UserInfo = uri.getUserInfo(); // get the Raw UserInfo String Raw_UserInfo = uri.getRawUserInfo(); // display the URI System.out.println("URI = " + uri); // display the UserInfo System.out.println(" UserInfo=" + _UserInfo); // display the UserInfo System.out.println(" Raw UserInfo=" + Raw_UserInfo); } // if any error occurs catch (URISyntaxException e) { // display the error System.out.println("URL Exception =" + e.getMessage()); } }}输出:
URI = https://Arnab_Kundu%E2%82%AC@www.geeksforgeeks.org UserInfo=Arnab_Kundu? Raw UserInfo=Arnab_Kundu%E2%82%AC
