Sending a POST Request with Parameters From a Java Class

This example shows how to sent a POST request to a server with attached parameters. Two parameters are sent in the example code below, width and height.
We use the URL and URLConnection classes to open the connection to the destination. Then the output stream is retrieved by calling getOutputStream() on the URLConnection object.
With the output stream we can write the parameters and then start reading the response from the server using the input stream which we get by calling getInputStream() on the same URLConnection object.
We assume there will only be character based content returned from the server so we use the BufferedWriter to read the response line by line.

Continue reading

Downloading a web page using URL and URLConnection classes

This Java code example shows how to download a webpage into a StringBuilder instance using the java.net.URL and java.net.URLConnection classes.
First we create an URL object specifying the url for the page as argument. URLConnection is an abstract class and cannot be instantiated so we need to call its static method openConnection()
to get an instance of it. Now the connection to the web page is open and we can start reading data from it.

Continue reading