Java API Tutorial - Java URLConnection.connect()








Syntax

URLConnection.connect() has the following syntax.

public abstract void connect()   throws IOException

Example

In the following code shows how to use URLConnection.connect() method.

import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
/*from  w w w .  ja  v a2 s  .com*/
public class Main {

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

    URL u = new URL("http://www.java2s.com");
    URLConnection uc = u.openConnection();
    uc.connect();
    System.out.println("Content-type: " + uc.getContentType());
    System.out.println("Content-encoding: " + uc.getContentEncoding());
    System.out.println("Date: " + new Date(uc.getDate()));
    System.out.println("Last modified: " + new Date(uc.getLastModified()));
    System.out.println("Expiration date: " + new Date(uc.getExpiration()));
    System.out.println("Content-length: " + uc.getContentLength());
  }

}

The code above generates the following result.