當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java URL getUserInfo()用法及代碼示例

getUserInfo()函數是URL類的一部分。函數getUserInfo()返回指定URL的UserInfo部分。

函數簽名

public String getUserInfo()

用法


url.getUserInfo()

參數:此函數不需要任何參數

返回類型:函數返回字符串類型

以下示例程序旨在說明getUserInfo()函數的用法:

例子1:給定一個URL,我們將使用getUserInfo()函數獲取UserInfo。

// Java program to show the use 
// of the function getUserInfo() 
  
import java.net.*; 
  
class Solution { 
    public static void main(String args[]) 
    { 
        // url  object 
        URL url = null; 
  
        try { 
            // create a URL 
            url = new URL( 
                "https:// Arnab_Kundu@www.geeksforgeeks.org"); 
  
            // get the  UserInfo 
            String _UserInfo = url.getUserInfo(); 
  
            // display the URL 
            System.out.println("URL = " + url); 
  
            // display the  UserInfo 
            System.out.println(" UserInfo= "
                               + _UserInfo); 
        } 
        // if any error occurs 
        catch (Exception e) { 
  
            // display the error 
            System.out.println(e); 
        } 
    } 
}
輸出:
URL = https:// Arnab_Kundu@www.geeksforgeeks.org
 UserInfo=  Arnab_Kundu

例子2:現在,我們將不再提供任何用戶信息,而是使用該函數獲取UserInfo並查看結果。

// Java program to show the use 
// of the function getUserInfo() 
  
import java.net.*; 
  
class Solution { 
    public static void main(String args[]) 
    { 
        // url  object 
        URL url = null; 
  
        try { 
            // create a URL 
            url = new URL("https:// www.geeksforgeeks.org"); 
  
            // get the  UserInfo 
            String _UserInfo = url.getUserInfo(); 
  
            // display the URL 
            System.out.println("URL = " + url); 
  
            // display the  UserInfo 
            System.out.println(" UserInfo= "
                               + _UserInfo); 
        } 
        // if any error occurs 
        catch (Exception e) { 
  
            // display the error 
            System.out.println(e); 
        } 
    } 
}
輸出:
URL = https:// www.geeksforgeeks.org
 UserInfo= null


相關用法


注:本文由純淨天空篩選整理自andrew1234大神的英文原創作品 URL getUserInfo() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。