URL Protocol

In this chapter you will learn:

  1. How to check the legal protocol

Protocol tester

The following code hard coded to-be-tested protocols in a String array. Then it create URL with the hard coded protocols. If the constructor throws out exception we know that the protocol is not supported.

import java.net.URL;
//from  ja  v  a2  s. co  m
public class MainClass {

  public static void main(String[] args) {

    String host = "www.java2s.com";
    String file = "/index.html";

    String[] schemes = { "http", "https", "ftp", "mailto", "telnet", "file", "ldap", "gopher",
        "jdbc", "rmi", "jndi", "jar", "doc", "netdoc", "nfs", "verbatim", "finger", "daytime",
        "systemresource" };

    for (int i = 0; i < schemes.length; i++) {
      try {
        URL u = new URL(schemes[i], host, file);
        System.out.println(schemes[i] + " is supported\r\n");
      } catch (Exception ex) {
        System.out.println(schemes[i] + " is not supported\r\n");
      }
    }
  }
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. How to read from URL with InputStreamReader
  2. Getting an Image from a URL
Home » Java Tutorial » URL URI
URL
URL Creation
URL for jar file
URL Components
Convert file path to URL
URL Relative
URL Protocol
Read from URL
Compare URL
URLConnection
HTTP Header
URLConnection Post
Cookie
URLConnection Read
HttpURLConnection
HttpURLConnection Properties
HttpURLConnection proxy
HttpURLConnection Authenticator
HTTPS
JarURLConnection
Encode a URL
Decode a URL
URI
URI Normalization
URI Resolution
URI Relativization
Convert URI to URL
IP Address
IP Ping
NetworkInterface