Get Port number from a URL in Java

Description

The following code shows how to get Port number from a URL.

Example


import java.net.URL;
//from   ww  w  .  j a  v a2  s. c  om
public class Main {
  public static void main(String[] args) throws Exception {
    URL url = new URL("http://www.yahoo.com:80/en/index.html?name=joe#first");
    System.out.println("prot:" + url.getPort());
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Network »




NetworkInterface
URI
URL
HTTP
HTTP Read
IP
Socket
UDP
URL Encode