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:com.manning.blogapps.chapter10.examples.AuthGetJava.java

public static void main(String[] args) throws Exception {
    if (args.length < 3) {
        System.out.println("USAGE: authget <username> <password> <url>");
        System.exit(-1);//from   w w w.  j av a2 s.  c o  m
    }
    String credentials = args[0] + ":" + args[1];

    URL url = new URL(args[2]);
    URLConnection conn = url.openConnection();

    conn.setRequestProperty("Authorization",
            "Basic " + new String(Base64.encodeBase64(credentials.getBytes())));

    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

    String s = null;
    while ((s = in.readLine()) != null) {
        System.out.println(s);
    }
}

From source file:Main.java

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

    long date = httpCon.getDate();
    if (date == 0)
        System.out.println("No date information.");
    else//from  w  w w  .j a v a  2s  .c  o  m
        System.out.println("Date: " + new Date(date));

}

From source file:Main.java

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

    InputStream inStrm = httpCon.getInputStream();
    System.out.println("\nContent at " + url);
    int ch;//from w  w w .java2 s.  c o m
    while (((ch = inStrm.read()) != -1))
        System.out.print((char) ch);
    inStrm.close();
}

From source file:Main.java

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

    long date = httpCon.getLastModified();
    if (date == 0)
        System.out.println("No last-modified information.");
    else//from   w ww  .  java2s . co  m
        System.out.println("Last-Modified: " + new Date(date));

}

From source file:Main.java

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

    long date = httpCon.getExpiration();
    if (date == 0)
        System.out.println("No expiration information.");
    else//from  w  w w .j a  v  a  2  s  .c  o  m
        System.out.println("Expires: " + new Date(date));

}

From source file:Main.java

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

    URL url = new URL("https://www.server.com");
    HttpURLConnection con = (HttpURLConnection) url.openConnection();

    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
    String inputLine;//from  w  w w  .  j a v  a 2  s. co m

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

From source file:Main.java

public static void main(String[] argv) throws Exception {
    HTMLDocument doc = new HTMLDocument() {
        public HTMLEditorKit.ParserCallback getReader(int pos) {
            return new HTMLEditorKit.ParserCallback() {
                public void handleText(char[] data, int pos) {
                    System.out.println(data);
                }/*from   www  . j  a  va2  s.  c o  m*/
            };
        }
    };

    URL url = new URI("http://www.google.com").toURL();
    URLConnection conn = url.openConnection();
    Reader rd = new InputStreamReader(conn.getInputStream());

    EditorKit kit = new HTMLEditorKit();
    kit.read(rd, doc, 0);
}

From source file:Main.java

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

    Map responseMap = connection.getHeaderFields();
    for (Iterator iterator = responseMap.keySet().iterator(); iterator.hasNext();) {
        String key = (String) iterator.next();
        System.out.println(key + " = ");

        List values = (List) responseMap.get(key);
        for (int i = 0; i < values.size(); i++) {
            Object o = values.get(i);
            System.out.println(o + ", ");
        }//from  ww  w.j  a v a 2 s . com
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Properties systemSettings = System.getProperties();
    systemSettings.put("proxySet", "true");
    systemSettings.put("http.proxyHost", "proxy.mycompany.local");
    systemSettings.put("http.proxyPort", "80");

    URL u = new URL("http://www.google.com");
    HttpURLConnection con = (HttpURLConnection) u.openConnection();
    BASE64Encoder encoder = new BASE64Encoder();
    String encodedUserPwd = encoder.encode("domain\\username:password".getBytes());
    con.setRequestProperty("Proxy-Authorization", "Basic " + encodedUserPwd);
    con.setRequestMethod("HEAD");
    System.out.println(con.getResponseCode() + " : " + con.getResponseMessage());
    System.out.println(con.getResponseCode() == HttpURLConnection.HTTP_OK);
}

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);

        if (headerName == null && headerValue == null) {
            break;
        }//from   ww w  .j  av  a  2s  .  c o m
        if ("Set-Cookie".equalsIgnoreCase(headerName)) {
            String[] fields = headerValue.split(";\\s*");
            for (int j = 1; j < fields.length; j++) {
                if ("secure".equalsIgnoreCase(fields[j])) {
                    System.out.println("secure=true");
                } else if (fields[j].indexOf('=') > 0) {
                    String[] f = fields[j].split("=");
                    if ("expires".equalsIgnoreCase(f[0])) {
                        System.out.println("expires" + f[1]);
                    } else if ("domain".equalsIgnoreCase(f[0])) {
                        System.out.println("domain" + f[1]);
                    } else if ("path".equalsIgnoreCase(f[0])) {
                        System.out.println("path" + f[1]);
                    }
                }
            }
        }
    }
}