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 url = new URL("http://www.google.com");
    HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();

    System.out.println("Response Message is " + httpCon.getResponseMessage());

}

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

    System.out.println("Request method is " + httpCon.getRequestMethod());

}

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

    System.out.println("Content-Type: " + httpCon.getContentType());

}

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();//  www .  j a  va  2s  .co  m
    DataInputStream di = new DataInputStream(urlConnection.getInputStream());

    FileOutputStream fo = new FileOutputStream("a.gif");
    while (-1 != di.read(b, 0, 1))
        fo.write(b, 0, 1);
    di.close();
    fo.close();
}

From source file:MainClass.java

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

    URL url = new URL("http://www.yourdomain.com/form.jsp");
    URLConnection connection = url.openConnection();
    connection.setDoOutput(true);/*from   ww  w  .j av  a2 s .c om*/
    PrintWriter out = new PrintWriter(connection.getOutputStream());
    out.println("firstName=Joe");
    out.println("lastName=Average");
    out.close();

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    URL url = new URL("http://www.java2s.com/");
    URLConnection urlConnection = url.openConnection();
    Map<String, List<String>> headers = urlConnection.getHeaderFields();
    Set<Map.Entry<String, List<String>>> entrySet = headers.entrySet();
    for (Map.Entry<String, List<String>> entry : entrySet) {
        String headerName = entry.getKey();
        System.out.println("Header Name:" + headerName);
        List<String> headerValues = entry.getValue();
        for (String value : headerValues) {
            System.out.print("Header value:" + value);
        }//from  w  w  w . j ava  2  s .  c o m
        System.out.println();
    }
}

From source file:Main.java

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

    URL google = new URL("http://www.google.com/");
    URLConnection googleConnection = google.openConnection();
    DataInputStream dis = new DataInputStream(googleConnection.getInputStream());
    StringBuffer inputLine = new StringBuffer();
    String tmp;/*from w w w  .j  a v a 2  s  . co  m*/
    while ((tmp = dis.readLine()) != null) {
        inputLine.append(tmp);
        System.out.println(tmp);
    }
    // use inputLine.toString(); here it would have whole source
    dis.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String data = URLEncoder.encode("key1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8");
    data += "&" + URLEncoder.encode("key2", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8");

    URL url = new URL("http://server.com:80/cgi");
    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);/*  ww w .ja v  a 2  s  .  co m*/
    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    wr.write(data);
    wr.flush();

    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line;
    while ((line = rd.readLine()) != null) {
        System.out.println(line);
    }
    wr.close();
    rd.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    URL url = new URL("http://www.java.com");
    URLConnection urlConnection = url.openConnection();
    HttpURLConnection connection = null;
    if (urlConnection instanceof HttpURLConnection) {
        connection = (HttpURLConnection) urlConnection;
    } else {//from  w  ww .j  a  v  a 2  s  .  com
        System.out.println("Please enter an HTTP URL.");
        return;
    }
    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String urlString = "";
    String current;
    while ((current = in.readLine()) != null) {
        urlString += current;
    }
    System.out.println(urlString);
}

From source file:Main.java

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

    URL url = new URL("http://www.java.com/");
    URLConnection urlConnection = url.openConnection();
    DataInputStream dis = new DataInputStream(urlConnection.getInputStream());
    String html = "", tmp = "";
    while ((tmp = dis.readUTF()) != null) {
        html += " " + tmp;
    }/*  w  w w.ja  v a  2s  .c  o  m*/
    dis.close();

    html = html.replaceAll("\\s+", " ");
    Pattern p = Pattern.compile("<title>(.*?)</title>");
    Matcher m = p.matcher(html);
    while (m.find() == true) {
        System.out.println(m.group(1));
    }
}