Java Networking - URLConnection

| Page Views: 56 java networking

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

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section.

About Me.

After completing his Software Engineering bachelors program, Oclemy(Clement Ochieng) these days is a man of two lives. At day he works for a startup in Nairobi, Kenya. At night he works tirelessly on building ProgrammingWizards TV, a tv channel for student coders and this website to help share the source code. In between he practices Meditation and Self actualization to help him keep balance. He also likes going for long solo walks to connect more with nature.




Recommendations


What do You Think


Previous Post Next Post