Java URI 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 :现在我们将不提供任何用户信息,而使用函数来获取用户信息并查看结果。
// 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
