Reading from a URLConnection
The following program explicitly retrieves a URLConnection
object and gets an input stream from the
connection. The connection is opened implicitly by calling getInputStream
. Then this program creates
a BufferedReader
on the input stream and reads from it:
import java.net.*; import java.io.*; public class URLConnectionReader { public static void main(String[] args) throws Exception { URL url = new URL("http://java.boot.by/"); URLConnection urlCon = url.openConnection(); BufferedReader in = new BufferedReader(new InputStreamReader(urlCon.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) { System.out.println(inputLine); } in.close(); } }
Writing to a URLConnection
Many HTML pages contain forms - text fields and other GUI objects that let you enter data to send to the server. After you type in the required information and initiate the query by clicking a button, your web browser writes the data to the URL over the network. At the other end the server receives the data, processes it, and then sends you a response, usually in the form of a new HTML page.
Many of these HTML forms use the HTTP POST METHOD to send data to the server. Thus writing to a URL is often called posting to a URL. The server recognizes the POST request and reads the data sent from the client.
For a Java program to interact with a server-side process it simply must be able to write to a URL, thus providing data to the server. It can do this by following these steps:
Create a URL
.
Retrieve the URLConnection
object.
Set output capability on the URLConnection
.
Open a connection to the resource.
Get an output stream from the connection.
Write to the output stream.
Close the output stream.
A program creates a URL object, and sets the connection so that it can write to it:
URL url = new URL("http://java.boot.by/servlet"); URLConnection connection = url.openConnection(); connection.setDoOutput(true);
The program then creates an output stream on the connection and opens an OutputStreamWriter
on it:
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
If the URL
does not support output, getOutputStream
method throws an
UnknownServiceException
. If the URL does support output, then this method returns an
output stream that is connected to the input stream of the URL on the server side - the client's output is the server's
input.
Next, the program writes the required information to the output stream and closes the stream:
out.write("Hello"); out.close();
This code writes to the output stream using the write
method. So you can see that writing data to a URL
is as easy as writing data to a stream. The data written to the output stream on the client side is the input for
the servlet on the server side.
Example of RESTful web service client:
public int getAge() { // Use the java.net.* APIs to access the Duke's Age RESTful web service HttpURLConnection connection = null; BufferedReader rd = null; StringBuilder sb = null; String line = null; URL serverAddress = null; try { serverAddress = new URL("http://localhost:8080/DukesAgeService/resources/dukesAge"); connection = (HttpURLConnection) serverAddress.openConnection(); connection.setRequestMethod("GET"); connection.setDoOutput(true); connection.setReadTimeout(10000); // Make the connection to Duke's Age connection.connect(); // Read in the response rd = new BufferedReader(new InputStreamReader(connection.getInputStream())); sb = new StringBuilder(); while ((line = rd.readLine()) != null) { sb.append(line); } // Convert the response to an int age = Integer.parseInt(sb.toString()); } catch (MalformedURLException e) { logger.warning("A MalformedURLException occurred."); e.printStackTrace(); } catch (ProtocolException e) { logger.warning("A ProtocolException occurred."); e.printStackTrace(); } catch (IOException e) { logger.warning("An IOException occurred"); e.printStackTrace(); } return age; }
![]() |
![]() ![]() |