Home | | Web Programming | HttpURLConnection - Java Networking

Chapter: Java The Complete Reference : The Java Library : Networking

HttpURLConnection - Java Networking

Java provides a subclass of URLConnection that provides support for HTTP connections. This class is called HttpURLConnection.

HttpURLConnection

 

Java provides a subclass of URLConnection that provides support for HTTP connections. This class is called HttpURLConnection. You obtain an HttpURLConnection in the same way just shown, by calling openConnection( ) on a URL object, but you must cast the result to HttpURLConnection. (Of course, you must make sure that you are actually opening an HTTP connection.) Once you have obtained a reference to an HttpURLConnection object, you can use any of the methods inherited from URLConnection. You can also use any of the several methods defined by HttpURLConnection. Here is a sampling:


 

The following program demonstrates HttpURLConnection. It first establishes a connection to www.google.com. Then it displays the request method, the response code, and the response message. Finally, it displays the keys and values in the response header.

 

// Demonstrate HttpURLConnection.

import java.net.*;

 

import java.io.*; import java.util.*;

 

class HttpURLDemo

 

{

 

public static void main(String args[]) throws Exception { URL hp = new URL(http://www.google.com");

 

HttpURLConnection hpCon = (HttpURLConnection) hp.openConnection();

 

// Display request method.

System.out.println("Request method is " +

hpCon.getRequestMethod());

 

// Display response code.

System.out.println("Response code is " +

hpCon.getResponseCode());

 

// Display response message.

System.out.println("Response Message is " +

 

hpCon.getResponseMessage());

 

    //Get a list of the header fields and a set

 

//of the header keys.

 

Map<String, List<String>> hdrMap = hpCon.getHeaderFields();

 

Set<String> hdrField = hdrMap.keySet();

 

System.out.println("\nHere is the header:");

 

// Display all header keys and values.

for(String k : hdrField) {

 

System.out.println("Key: " + k +

 

    Value: " + hdrMap.get(k));

 

}

 

}

 

}

 

The output produced by the program is shown here. (Of course, the exact response returned by www.google.com will vary over time.)

 

Request method is GET

 

Response code is 200

 

Response Message is OK

 

Here is the header:

 

Key: Transfer-Encoding      Value: [chunked]

Key: X-Frame-Options Value: [SAMEORIGIN] Key: null Value: [HTTP/1.1 200 OK]

Key: Server Value: [gws]

 

Key: Cache-Control Value: [private, max-age=0] Key: Set-Cookie Value:

[NID=67=rMTQWvn5eVIYA2d8F5Iu_8L-68wiMACyaXYqeSe1bvR8SzQQ_PaDCy5mNbxuw5XtdcjY KIwmy3oVJM1Y0qZdibBOkQfJmtHpAtO61GVwumQ1ApgSXWjZ67yHxQX3g3-h; expires=Wed, 23-Apr-2014 18:31:09 GMT; path=/; domain=.google.com; HttpOnly, PREF=ID=463b5df7b9ced9d8:FF=0:TM=1382466669:LM=1382466669:S=3LI-oT-Dzi46U1On ; expires=Thu, 22-Oct-2015 18:31:09 GMT; path=/; domain=.google.com]

 

Key: Expires Value: [-1]

 

Key: X-XSS-Protection        Value: [1; mode=block]

 

Key: P3P Value: [CP=”This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info.”]

Key: Date  Value: [Tue, 22 Oct 2013 18:31:09 GMT]

 

Key: Content-Type      Value: [text/html;charset=ISO-8859-1]

 

Notice how the header keys and values are displayed. First, a map of the header keys and values is obtained by calling getHeaderFields( ) (which is inherited from URLConnection). Next, a set of the header keys is retrieved by calling keySet( ) on the map. Then, the key set is cycled through by using a for-each style for loop. The value associated with each key is obtained by calling get( ) on the map.

The URI Class

The URI class encapsulates a Uniform Resource Identifier (URI). URIs are similar to URLs. In fact, URLs constitute a subset of URIs. A URI represents a standard way to identify a resource. A URL also describes how to access the resource.


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Java The Complete Reference : The Java Library : Networking : HttpURLConnection - Java Networking |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.