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 {
    String urlString = "http://google.com";
    CookieManager manager = new CookieManager();
    CookieHandler.setDefault(manager);
    URL url = new URL(urlString);
    URLConnection connection = url.openConnection();
    Object obj = connection.getContent();
    url = new URL(urlString);
    connection = url.openConnection();// w  ww.j  av  a  2 s . com
    obj = connection.getContent();
    CookieStore cookieJar = manager.getCookieStore();
    List<HttpCookie> cookies = cookieJar.getCookies();
    for (HttpCookie cookie : cookies) {
        System.out.println(cookie);
    }
}

From source file:FetchCookie.java

public static void main(String args[]) throws Exception {
    String urlString = "http://java.sun.com";
    CookieManager manager = new CookieManager();
    CookieHandler.setDefault(manager);
    URL url = new URL(urlString);
    URLConnection connection = url.openConnection();
    Object obj = connection.getContent();
    url = new URL(urlString);
    connection = url.openConnection();//from  w ww.ja v a  2s.c  o  m
    obj = connection.getContent();
    CookieStore cookieJar = manager.getCookieStore();
    List<HttpCookie> cookies = cookieJar.getCookies();
    for (HttpCookie cookie : cookies) {
        System.out.println(cookie);
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    URL url = new URL("http://hostname:80");
    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;
        }// w w  w  .j av a  2  s  .c  o  m
    }
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Date today = new Date();
    long millisecondsPerDay = 24 * 60 * 60 * 1000;

    URL u = new URL("http://www.java2s.com");
    URLConnection uc = u.openConnection();
    uc.setIfModifiedSince((new Date(today.getTime() - millisecondsPerDay)).getTime());
    InputStream in = new BufferedInputStream(uc.getInputStream());
    Reader r = new InputStreamReader(in);
    int c;//from w ww  . ja  v  a 2s.  c  o m
    while ((c = r.read()) != -1) {
        System.out.print((char) c);
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    String fullURL = args[0];/* w w  w.  j a  v  a2s. com*/
    URL u = new URL(fullURL);
    URLConnection conn = u.openConnection();
    conn.setDoInput(true);
    OutputStream theControl = conn.getOutputStream();
    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(theControl));
    for (int i = 1; i < args.length; i++) {
        out.write(args[i] + "\n");
    }
    out.close();
    InputStream theData = conn.getInputStream();

    String contentType = conn.getContentType();
    if (contentType.toLowerCase().startsWith("text")) {
        BufferedReader in = new BufferedReader(new InputStreamReader(theData));
        String line;
        while ((line = in.readLine()) != null) {
            System.out.println(line);
        }
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    String fullURL = args[0];// w  w  w.  ja  v  a  2s .c o m
    URL u = new URL(fullURL);
    URLConnection conn = u.openConnection();
    conn.setDoOutput(true);
    OutputStream theControl = conn.getOutputStream();
    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(theControl));
    for (int i = 1; i < args.length; i++) {
        out.write(args[i] + "\n");
    }
    out.close();
    InputStream theData = conn.getInputStream();

    String contentType = conn.getContentType();
    if (contentType.toLowerCase().startsWith("text")) {
        BufferedReader in = new BufferedReader(new InputStreamReader(theData));
        String line;
        while ((line = in.readLine()) != null) {
            System.out.println(line);
        }
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    URL url = new URL("http://www.google.com");
    URLConnection httpCon = (URLConnection) url.openConnection();

    int s = httpCon.getHeaderFieldInt("intKey", 1);

}

From source file:Main.java

public static void main(String args[]) throws Exception {
    URL url = new URL("http://www.google.com");
    URLConnection httpCon = (URLConnection) url.openConnection();

    long s = httpCon.getHeaderFieldLong("longKey", 1L);

}

From source file:Main.java

public static void main(String args[]) throws Exception {
    URL url = new URL("http://www.google.com");
    URLConnection httpCon = (URLConnection) url.openConnection();

    System.out.println(URLConnection.guessContentTypeFromStream(httpCon.getInputStream()));

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    URL url = new URL("jar:file:/c://my.jar!/");
    JarURLConnection conn = (JarURLConnection) url.openConnection();
    JarFile jarfile = conn.getJarFile();
}