Get content type for a URL connection in Java

Description

The following code shows how to get content type for a URL connection.

Example


//w  ww.j  a  v a 2  s  .  c om
import java.net.URL;
import java.net.URLConnection;

public class Main {

  public static void main(String args[]) throws Exception {

    URL u = new URL("http://www.google.com");
    URLConnection uc = u.openConnection();
    System.out.println("Content-type: " + uc.getContentType());
  }

}

The code above generates the following result.





















Home »
  Java Tutorial »
    Network »




NetworkInterface
URI
URL
HTTP
HTTP Read
IP
Socket
UDP
URL Encode