📜  Java中的 URL getProtocol() 方法及示例

📅  最后修改于: 2022-05-13 01:54:34.138000             🧑  作者: Mango

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