Sunday 11 October 2015

HttpURLConnection: work with HTTP urls

HttpURLConnection is a sub class of URLConnection class, and provides some extra functionality to work with http urls.

How to get instance of HttpURLConnection object
Since HttpURLConnection is an abstract class, we can’t instantiate it directly. We can instantiate it, by calling openConnection() method of URL class.
/**
   * 
   * @param resource Resource to connect to
   * @return HttpURLConnection object for this resource
   */
  public static HttpURLConnection getConnection(String resource) {

    URL url = null;
    HttpURLConnection connection = null;
    try {
      url = new URL(resource);
      connection = (HttpURLConnection) url.openConnection();
    } catch (MalformedURLException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

    return connection;
  }

By default HttpURLConnection uses GET method, you can change the request method by using setRequest() method.

public void setRequestMethod(String method) throws ProtocolException
Set the method for the URL request, method is one of GET, POST, HEAD, OPTIONS, PUT, DELETE, TRACE


Following application gets the headers of given resource.

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HttpURLConnectionUtil {

  /**
   * 
   * @param resource
   *            Resource to connect to
   * @return HttpURLConnection object for this resource
   */
  public static HttpURLConnection getConnection(String resource) {

    URL url = null;
    HttpURLConnection connection = null;
    try {
      url = new URL(resource);
      connection = (HttpURLConnection) url.openConnection();
    } catch (MalformedURLException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

    return connection;
  }

  /**
   * @param connection
   * @return map of header fields
   */
  public static Map<String, List<String>> getHeaders(
      HttpURLConnection connection) {

    try {
      connection.setRequestMethod("HEAD");
      Map<String, List<String>> headersMap = connection.getHeaderFields();
      return headersMap;

    } catch (ProtocolException e) {
      e.printStackTrace();
    }

    return new HashMap<String, List<String>>();
  }

}


import java.net.HttpURLConnection;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Main {

  public static void main(String args[]) {
    String resource = "https://self-learning-java-tutorial.blogspot.com";

    HttpURLConnection connection = HttpURLConnectionUtil
        .getConnection(resource);
    Map<String, List<String>> headersMap = HttpURLConnectionUtil
        .getHeaders(connection);

    Set<String> headersSet = headersMap.keySet();
    for (String header : headersSet) {
      System.out.println(header + " " + headersMap.get(header));
    }
    
    connection.disconnect();

  }
}


Sample Output
null [HTTP/1.1 200 OK]
ETag ["48d68749-2249-4b04-8401-c716a53d1936"]
Date [Fri, 22 May 2015 06:25:19 GMT]
Content-Length [0]
X-XSS-Protection [1; mode=block]
Expires [Fri, 22 May 2015 06:25:19 GMT]
Alternate-Protocol [80:quic,p=0]
Last-Modified [Fri, 22 May 2015 03:51:54 GMT]
Content-Type [text/html; charset=UTF-8]
Server [GSE]
X-Content-Type-Options [nosniff]
Cache-Control [private, max-age=0]



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment