Example usage for java.net URLConnection connect

List of usage examples for java.net URLConnection connect

Introduction

In this page you can find the example usage for java.net URLConnection connect.

Prototype

public abstract void connect() throws IOException;

Source Link

Document

Opens a communications link to the resource referenced by this URL, if such a connection has not already been established.

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.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());
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    byte[] b = new byte[1];
    URL url = new URL("http://www.server.com/a.gif");
    URLConnection urlConnection = url.openConnection();
    urlConnection.connect();
    DataInputStream di = new DataInputStream(urlConnection.getInputStream());

    FileOutputStream fo = new FileOutputStream("a.gif");
    while (-1 != di.read(b, 0, 1))
        fo.write(b, 0, 1);/*from  w  w  w  . j  ava  2  s  .c  om*/
    di.close();
    fo.close();
}

From source file:Main.java

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

    conn.setRequestProperty("Cookie", "name1=value1; name2=value2");

    conn.connect();
}

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 source file:Main.java

License:asdf

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

    URLConnection conn = new URL("http://www.yourserver.com").openConnection();
    conn.setDoInput(true);//  w w w  . j av  a 2  s . c  o  m
    conn.setRequestProperty("Authorization", "asdfasdf");
    conn.connect();

    InputStream in = conn.getInputStream();
}

From source file:SendMail.java

public static void main(String[] args) {
    try {//from  w ww.  j ava 2  s  . c om
        // If the user specified a mailhost, tell the system about it.
        if (args.length >= 1)
            System.getProperties().put("mail.host", args[0]);

        // A Reader stream to read from the console
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

        // Ask the user for the from, to, and subject lines
        System.out.print("From: ");
        String from = in.readLine();
        System.out.print("To: ");
        String to = in.readLine();
        System.out.print("Subject: ");
        String subject = in.readLine();

        // Establish a network connection for sending mail
        URL u = new URL("mailto:" + to); // Create a mailto: URL
        URLConnection c = u.openConnection(); // Create its URLConnection
        c.setDoInput(false); // Specify no input from it
        c.setDoOutput(true); // Specify we'll do output
        System.out.println("Connecting..."); // Tell the user
        System.out.flush(); // Tell them right now
        c.connect(); // Connect to mail host
        PrintWriter out = // Get output stream to host
                new PrintWriter(new OutputStreamWriter(c.getOutputStream()));

        // We're talking to the SMTP server now.
        // Write out mail headers. Don't let users fake the From address
        out.print("From: \"" + from + "\" <" + System.getProperty("user.name") + "@"
                + InetAddress.getLocalHost().getHostName() + ">\r\n");
        out.print("To: " + to + "\r\n");
        out.print("Subject: " + subject + "\r\n");
        out.print("\r\n"); // blank line to end the list of headers

        // Now ask the user to enter the body of the message
        System.out.println("Enter the message. " + "End with a '.' on a line by itself.");
        // Read message line by line and send it out.
        String line;
        for (;;) {
            line = in.readLine();
            if ((line == null) || line.equals("."))
                break;
            out.print(line + "\r\n");
        }

        // Close (and flush) the stream to terminate the message
        out.close();
        // Tell the user it was successfully sent.
        System.out.println("Message sent.");
    } catch (Exception e) { // Handle any exceptions, print error message.
        System.err.println(e);
        System.err.println("Usage: java SendMail [<mailhost>]");
    }
}

From source file:URLConnectionTest.java

public static void main(String[] args) {
    try {/*from  www.  j  a  v a  2  s . c  o m*/
        String urlName;
        if (args.length > 0)
            urlName = args[0];
        else
            urlName = "http://java.sun.com";

        URL url = new URL(urlName);
        URLConnection connection = url.openConnection();

        // set username, password if specified on command line

        if (args.length > 2) {
            String username = args[1];
            String password = args[2];
            String input = username + ":" + password;
            String encoding = base64Encode(input);
            connection.setRequestProperty("Authorization", "Basic " + encoding);
        }

        connection.connect();

        // print header fields

        Map<String, List<String>> headers = connection.getHeaderFields();
        for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
            String key = entry.getKey();
            for (String value : entry.getValue())
                System.out.println(key + ": " + value);
        }

        // print convenience functions

        System.out.println("----------");
        System.out.println("getContentType: " + connection.getContentType());
        System.out.println("getContentLength: " + connection.getContentLength());
        System.out.println("getContentEncoding: " + connection.getContentEncoding());
        System.out.println("getDate: " + connection.getDate());
        System.out.println("getExpiration: " + connection.getExpiration());
        System.out.println("getLastModifed: " + connection.getLastModified());
        System.out.println("----------");

        Scanner in = new Scanner(connection.getInputStream());

        // print first ten lines of contents

        for (int n = 1; in.hasNextLine() && n <= 10; n++)
            System.out.println(in.nextLine());
        if (in.hasNextLine())
            System.out.println(". . .");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.maverick.ssl.SSLSocket.java

public static void main(String[] args) {

    try {/*from  w  ww.j a va2s.co m*/

        HttpsURLStreamHandlerFactory.addHTTPSSupport();

        URL url = new URL("https://localhost"); //$NON-NLS-1$

        URLConnection con = url.openConnection();
        con.setDoInput(true);
        con.setDoOutput(false);
        con.setAllowUserInteraction(false);

        con.setRequestProperty("Accept", //$NON-NLS-1$
                "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*"); //$NON-NLS-1$
        con.setRequestProperty("Accept-Encoding", "gzip, deflate"); //$NON-NLS-1$ //$NON-NLS-2$
        con.setRequestProperty("Accept-Language", "en-gb"); //$NON-NLS-1$ //$NON-NLS-2$
        con.setRequestProperty("Connection", "Keep-Alive"); //$NON-NLS-1$ //$NON-NLS-2$
        con.setRequestProperty("User-Agent", //$NON-NLS-1$
                "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)"); //$NON-NLS-1$

        con.connect();

        InputStream in = con.getInputStream();
        int read;

        while ((read = in.read()) > -1) {
            System.out.write(read);
        }

    } catch (SSLIOException ex) {
        ex.getRealException().printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:GetURLInfo.java

/** Use the URLConnection class to get info about the URL */
public static void printinfo(URL url) throws IOException {
    URLConnection c = url.openConnection(); // Get URLConnection from URL
    c.connect(); // Open a connection to URL

    // Display some information about the URL contents
    System.out.println("  Content Type: " + c.getContentType());
    System.out.println("  Content Encoding: " + c.getContentEncoding());
    System.out.println("  Content Length: " + c.getContentLength());
    System.out.println("  Date: " + new Date(c.getDate()));
    System.out.println("  Last Modified: " + new Date(c.getLastModified()));
    System.out.println("  Expiration: " + new Date(c.getExpiration()));

    // If it is an HTTP connection, display some additional information.
    if (c instanceof HttpURLConnection) {
        HttpURLConnection h = (HttpURLConnection) c;
        System.out.println("  Request Method: " + h.getRequestMethod());
        System.out.println("  Response Message: " + h.getResponseMessage());
        System.out.println("  Response Code: " + h.getResponseCode());
    }/*from www  .  j  a  v a2  s .  c  o m*/
}

From source file:Main.java

public static Drawable getDrawable(String urlpath) {
    Drawable d = null;//from   w ww.  j  a  v a 2s.  c  o m
    try {
        URL url = new URL(urlpath);
        URLConnection conn = url.openConnection();
        conn.connect();
        InputStream in;
        in = conn.getInputStream();
        d = Drawable.createFromStream(in, "background.jpg");
        // TODO Auto-generated catch block
    } catch (IOException e) {
        e.printStackTrace();
    }
    return d;
}