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


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


getProtocol()函數是URL類的一部分。函數getProtocol()返回指定URL的協議。

函數簽名

public String getProtocol()

用法


url.getProtocol()

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

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

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

範例1:

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


相關用法


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