当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。