Tag Archives: URL

Getting the HTTP headers from an URL



Introduction

In an other article we already read from the URL. In this example only the HTTP headers are read using the same URL object:

try {
    // Create a URLConnection object for a URL
    URL url = new URL("http://hostname:80");
    URLConnection conn = url.openConnection();

    // List all the response headers from the
    // server.
    // Note: The first call to
    // getHeaderFieldKey() will implicit send
    // the HTTP request to the server.
    for (int i=0; ; i++) {
        String headerName =
        conn.getHeaderFieldKey(i);
        String headerValue =
        conn.getHeaderField(i);
        if (headerName == null && 
            headerValue == null) {
            // No more headers
            break;
        }
        if (headerName == null) {
            // The header value contains the
              server's HTTP version
        }
    }
} catch (Exception e) {
}

 

Converting from a URL to a URI

Introduction

The URI class  represents a Unified Recourse Indicator. The URL represents a Unified Resource Locator.

What’s the difference?

URI can be further classified as a locator, a name, or both. The term “Uniform Resource Locator” (URL) refers to the subset of URIs that, in addition to identifying a resource, provide a means of locating the resource by describing its primary access mechanism (e.g., its network “location”). In other words; the URI is more specific than the URL. However. The W3C realized that there is a ton of confusion about this. They issued a URI clarification document that says that it is now OK to use the terms URL and URI interchangeably (to mean URI). It is no longer useful to strictly segment URIs into different types, which means that according to W3C there is no longer a difference between the two. However they have different Classes. The example below shows how to convert between the two.

URI uri = null;
URL url = null;

// Create a URI
try {
    uri = new URI("file://index.html");
} catch (URISyntaxException e) {
}

// Convert an absolute URI to a URL
try {
    url = uri.toURL();
} catch (IllegalArgumentException e) {
    // URI was not absolute
} catch (MalformedURLException e) {
}

// Convert a URL to a URI
try {
    uri = new URI(url.toString());
} catch (URISyntaxException e) {
}

 

The java.net.URL

evertwagenaar.com logo

 

Introduction

Above everything the Java Programming language was designed to

be a Networking Language by Sun Microsystems early nineties Sun’s slogan was “The Network is the Computer”.

Actually John Gage wasn’t completely right. Although it has become true for some systems   Computers are still the Computer, weather they are smartphones, smart watches, PC’s, Macs, iPads, Android tablets or Virtual Machines are still the real computers. Although they have become more and more connected and serve the API’s which  are accessed trough the networks.

Public API’s are becoming more and more available and are used as services which indeed make the Network. The coming of SOA slowly is making things better.

Time to see how you can connect to a (web) service using the java.net API.

Opening and reading from a url

In java.net the Opbject which represents an url is the URL object. You can instantiate it as follows:

URL url = new URL("https://evertwagenaar.com");

The next step is to open a connection:

HTTPUrlConnection con = url.openConnection();

Now we are ready to read from the connection. This is similar to reading from a file:

 

BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine())
          != null) 
          System.out.println(inputLine);
        in.close();
    }
}

This prints the html of my homepage to the console. If you want to write it to disk, follow the instructions on writing to a file.

As you can see, the methods for java.io are consistent everywhere, which makes the Java programming language easy to learn.

i/o streams should always be closed!!!

Conclusion

This article demonstrates how to make a http connection and read a full HTML page from an HTTP connection using only plain Java objects. In another article we will post something to the http server.