Java Networking - URLConnection

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. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think


Previous Post Next Post