Sunday, March 31, 2013

How to get response header from HTTP request



package com.swain.cell;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class HttpResponseHeader {

       public static void main(String[] args) {
              try {
                     URL url = new URL("http://corejavaexample.blogspot.in/");
                     URLConnection connection = url.openConnection();

                     Map responseMap = connection.getHeaderFields();
                     for (Iterator iterator = responseMap.keySet().iterator(); iterator
                                  .hasNext();) {
                           String key = (String) iterator.next();
                           System.out.print(key + " = ");

                           List values = (List) responseMap.get(key);
                           for (int i = 0; i < values.size(); i++) {
                                  Object o = values.get(i);
                                  System.out.print(o + ", ");
                           }
                           System.out.println("");
                     }
              } catch (MalformedURLException e) {
                     e.printStackTrace();
              } catch (IOException e) {
                     e.printStackTrace();
              }
       }

}

Java Quick Program

1. Write a Code to generate Random numbers public class GenerateRandomNumbers { public static void main(String[] args) { System.out...