Java URI 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.orgProtocol = httpsURL = http:// www.geeksforgeeks.orgProtocol = httpURL = ftp:// www.geeksforgeeks.orgProtocol = ftp
