In the previous article, we saw how we can create a URL object and get the information of any resource on the internet. But barely getting the state-information is not the true motive of a real world application. To retrieve the information, process it and sending the results back to the server, or just display the required information retrieved from the server is what we are aiming at. Consider, for example a small application which asks for a movie name from user and in turn returns the “imdb” rating of the movie or return all the links related to that movie. All of this can be achieved using the URLConnection class.
What is URLConnection class?
URLConnection is an abstract class whose subclasses form the link between the user application and any resource on the web. We can use it to read/write from/to any resource referenced by a URL object.
There are mainly two subclass that extends the URLConnection class-
- HttpURLConnection: If we are connecting to any url which uses “http” as its protocol, then HttpURLConnection class is used.
- JarURLConnection: If however, we are trying to establish a connection to a jar file on the web, then JarURLConnection is used.
Once the connection is established and we have a URLConnection object, we can use it to read or write or get further information about when was the page/file last modified, content length etc.
- URLConnection openConnection(): opens the connection to the specified URL.
- Object getContent(): retrieves the content of the URLConnection.
- Map<String,List> getHeaderFields(): returns the map containing the values of various header fields in the http header.
- getContentEncoding(): Returns the value of the content-encoding header field.
- getContentLength(): returns the length of the content header field.
- getDate(): returns value of date in header field.
- getHeaderField(int i): returns the value of ith index of header.
- getHeaderField(String field): returns the value of field named “field” in header
To get the list of all header fields, read this.
- OutputStream getOutputStream(): returns the output stream to this connection.
- InputStream getInputStream(): returns the input stream to this open connection.
- setAllowUserInteraction(boolean): Setting this true means a user can interact with the page. Default value is true.
- setDefaultUseCaches(boolean): Sets the default value of useCache field as the given value.
- setDoInput(boolean): sets if the user is allowed to take input or not.
- setDoOutput(boolean): sets if the user is allowed to write on the page. Default value is false since most of the url dont allow to write.
Lets look at a sample program, which uses the above methods to display the header fields and also print the source code of entire page on to the console window.
Keep-Alive : [timeout=5, max=100] null : [HTTP/1.1 200 OK] Server : [Apache/2.4.18 (Ubuntu)] Connection : [Keep-Alive] Last-Modified : [Wed, 16 Nov 2016 06:49:55 GMT] Date : [Wed, 16 Nov 2016 10:58:34 GMT] Accept-Ranges : [bytes] Cache-Control : [max-age=3] ETag : ["10866-541657b07e4d7"] Vary : [Accept-Encoding] Expires : [Wed, 16 Nov 2016 10:58:37 GMT] Content-Length :  Content-Type : Complete source code of the URL is- -------------------------------------------------- ...source code of the page...
Steps involved in the above process:
- URL Creation: Create a URL object using any of the constructors given int
- this article
- Create Object: Invoke the openConnection() call to create the object of URLConnection.
- Display the Content: Either use the above created object to display the information about the resource or to read/write contents of the file to console using bufferedReader and InputStream of the open connection using getInputStream() method.
- Close Stream: Close the InputStream when done.
This article is contributed by Rishabh Mahrsee. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Inner Class And Anonymous Inner Class that Implements Runnable | Concurrent Programming Approach 3
- Implement Quintet Class with Quartet Class in Java using JavaTuples
- Implement Triplet Class with Pair Class in Java using JavaTuples
- Implement Sextet Class from Quintet Class in Java using JavaTuples
- Implement Octet Class from Septet Class in Java using JavaTuples
- Implement Quartet Class with Triplet Class in Java using JavaTuples
- Implement Ennead Class from Octet Class in Java using JavaTuples
- Implement Pair Class with Unit Class in Java using JavaTuples
- Implement Septet Class from Sextet Class in Java using JavaTuples
- Implement Decade Class from Ennead Class in Java using JavaTuples
- Difference between Abstract Class and Concrete Class in Java
- In Java, Can we call the main() method of a class from another class?
- Using predefined class name as Class or Variable name in Java
- Reading a CSV file in Java using OpenCV
- Different ways of Reading a text file in Java