Java – All in One → Java Networking – URLConnection – Camposha

Java – All in One

Java – All in One

Java – All in One

Java Networking – URLConnection

 

URLConnection is a java class used to connect to a URL and perform reads and writes.

Uses of URLConnection

Well, connect to a URL then write to or read from it.

Characteristics of URLConnection class

  1. First it belongs to java.net package.
  2. It’s an abstract class:
    public abstract class URLConnection..{}
  3. It derives from java.lang.Object:
    public abstract class URLConnection extends Object{}

URLConnection children

This class has several children:

Class Type Description
HttpURLConnection Direct URLConnection for HTTP.
JarURLConnection Direct URLConnection for jars
HttpsURLConnection Indirect This is the HttpURLConnection for HTTPS.

Examples

1. How to Open a URL Connection
    private static URLConnection open(String url, String auth) throws MalformedURLException, IOException {
        URLConnection conn = new URL(url).openConnection();
        conn.setDoOutput(true);
        conn.setRequestProperty("Accept-Charset", charset);
        conn.setRequestProperty("Content-Type", contentType);
        if (auth != null) {
            conn.setRequestProperty("Authorization", auth);
        }
        return conn;
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

Rating

Not enough ratings to display
Join Us
X