Example usage for java.net URL openConnection

List of usage examples for java.net URL openConnection

Introduction

In this page you can find the example usage for java.net URL openConnection.

Prototype

public URLConnection openConnection() throws java.io.IOException 

Source Link

Document

Returns a java.net.URLConnection URLConnection instance that represents a connection to the remote object referred to by the URL .

Usage

From source file:Main.java

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

    URL u = new URL("http://www.java2s.com");
    URLConnection uc = u.openConnection();

    uc.setIfModifiedSince(1000);//from  w  w  w .  ja v a 2 s. co  m
    System.out.println(uc.getIfModifiedSince());
}

From source file:Main.java

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

    URL u = new URL("http://www.java2s.com");
    URLConnection uc = u.openConnection();

    uc.setConnectTimeout(1000);//from  ww w .j av  a  2  s. com
    System.out.println(uc.getConnectTimeout());
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    URL url = new URL("http://www.x.com");
    URLConnection urlc = url.openConnection();
    urlc.setRequestProperty("User-Agent", "Mozilla 5.0 (Windows; U; " + "Windows NT 5.1; en-US; rv:1.8.0.11) ");

    InputStream is = urlc.getInputStream();
    int c;/*www.  j  a va  2 s .  c  o m*/
    while ((c = is.read()) != -1)
        System.out.print((char) c);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    HttpURLConnection.setFollowRedirects(false);

    URL url = new URL("http://hostname:80");
    URLConnection conn = url.openConnection();

    HttpURLConnection httpConn = (HttpURLConnection) conn;
    httpConn.setInstanceFollowRedirects(false);

    conn.connect();/*from   w  ww  .  j a  v a  2s  .  c  o  m*/
}

From source file:Main.java

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

    URL u = new URL("http://www.java2s.com");
    URLConnection uc = u.openConnection();
    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());
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    URL yahoo = new URL("http://www.yahoo.com/");
    URLConnection yc = yahoo.openConnection();
    BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
    String inputLine;//  ww  w . j  a  v  a  2s.c  om

    while ((inputLine = in.readLine()) != null)
        System.out.println(inputLine);
    in.close();
}

From source file:Main.java

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

    URL u = new URL("http://www.java2s.com");
    URLConnection uc = u.openConnection();
    uc.connect();//from   www  . j a  va 2s  .c o m
    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());
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    int size;/* ww  w .  j a  v  a2  s  .co  m*/
    URL url = new URL("http://www.server.com");
    URLConnection conn = url.openConnection();
    size = conn.getContentLength();
    if (size < 0)
        System.out.println("Could not determine file size.");
    else
        System.out.println(size);
    conn.getInputStream().close();

}

From source file:MainClass.java

public static void main(String args[]) {
    try {/* w  w w  .j a va 2 s .c o  m*/
        URL u = new URL("http://www.java2s.com");
        URLConnection uc = u.openConnection();
        System.out.println("Content-encoding: " + uc.getContentEncoding());
    } catch (MalformedURLException e) {
        System.err.println("not a URL I understand");
    } catch (IOException e) {
        System.err.println(e);
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    URL url = new URL("http://java2s.com");
    URLConnection conn = url.openConnection();

    for (int i = 0;; i++) {
        String headerName = conn.getHeaderFieldKey(i);
        String headerValue = conn.getHeaderField(i);
        System.out.println(headerName);
        System.out.println(headerValue);

        if (headerName == null && headerValue == null) {
            System.out.println("No more headers");
            break;
        }/*from ww w.ja va 2 s. c  o  m*/
    }
}