HttpHeader.java
1 // HTMLParser Library $Name: v1_6_20060319 $ - A java-based parser for HTML 2 // http://sourceforge.org/projects/htmlparser 3 // Copyright (C) 2004 Derrick Oswald 4 // 5 // Revision Control Information 6 // 7 // $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/http/HttpHeader.java,v $ 8 // $Author: derrickoswald $ 9 // $Date: 2005/06/19 12:01:13 $ 10 // $Revision: 1.1 $ 11 // 12 // This library is free software; you can redistribute it and/or 13 // modify it under the terms of the GNU Lesser General Public 14 // License as published by the Free Software Foundation; either 15 // version 2.1 of the License, or (at your option) any later version. 16 // 17 // This library is distributed in the hope that it will be useful, 18 // but WITHOUT ANY WARRANTY; without even the implied warranty of 19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 20 // Lesser General Public License for more details. 21 // 22 // You should have received a copy of the GNU Lesser General Public 23 // License along with this library; if not, write to the Free Software 24 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 25 // 26 27 package org.htmlparser.http; 28 29 import java.io.IOException; 30 import java.net.HttpURLConnection; 31 import java.util.Iterator; 32 import java.util.List; 33 import java.util.Map; 34 35 /** 36 * Utility methods to display HTTP headers. 37 */ 38 public class HttpHeader 39 { 40 41 /** 42 * Private constructor. 43 * This class is completely static. 44 */ 45 private HttpHeader () 46 { 47 } 48 49 /** 50 * Gets the request header for the connection. 51 * <em>This header is generated from the contents of the connection 52 * and may not be exactly the same as the request that will be sent.</em> 53 * @param connection The connection to convert into an HTTP request header. 54 * @return The string that would be sent by the HTTP request. 55 */ 56 public static String getRequestHeader (HttpURLConnection connection) 57 { 58 // dump it 59 StringBuffer buffer; 60 Map map; 61 String key; 62 List items; 63 64 buffer = new StringBuffer (1024); 65 buffer.append (connection.getRequestMethod ()); 66 buffer.append (" "); 67 buffer.append (connection.getURL ()); 68 buffer.append (" HTTP/1.1\n"); 69 map = connection.getRequestProperties (); 70 for (Iterator iter = map.keySet ().iterator (); iter.hasNext (); ) 71 { 72 key = (String)iter.next (); 73 items = (List)map.get (key); 74 buffer.append (key); 75 buffer.append (": "); 76 for (int i = 0; i < items.size (); i++) 77 { 78 if (0 != i) 79 buffer.append (", "); 80 buffer.append (items.get (i)); 81 } 82 buffer.append ("\n"); 83 } 84 85 return (buffer.toString ()); 86 } 87 88 /** 89 * Gets the response header for the connection. 90 * Calling this method on an un-connected connection will 91 * generate an error, as will an attempt to get information 92 * from a connected but invalid connection. 93 * <em>This header is generated from the contents of the connection 94 * and may not be exactly the same as the response that was received.</em> 95 * @param conn The connection to convert into an HTTP response header. 96 * @return The string that was sent as the HTTP response. 97 */ 98 public static String getResponseHeader (HttpURLConnection conn) 99 { 100 // dump it 101 StringBuffer buffer; 102 int code; 103 String message; 104 String key; 105 String value; 106 107 buffer = new StringBuffer (1024); 108 try 109 { 110 code = conn.getResponseCode (); 111 if (-1 != code) 112 { 113 message = conn.getResponseMessage (); 114 buffer.append ("HTTP/1.1 "); 115 buffer.append (code); 116 buffer.append (" "); 117 buffer.append (message); 118 buffer.append ("\n"); 119 for (int i = 0; null != (value = conn.getHeaderField (i)); i++) 120 { 121 key = conn.getHeaderFieldKey (i); 122 if (null != key) 123 { 124 buffer.append (key); 125 buffer.append (": "); 126 buffer.append (value); 127 buffer.append ("\n"); 128 } 129 } 130 } 131 } 132 catch (IOException ioe) 133 { 134 buffer.append (ioe.toString ()); 135 } 136 137 return (buffer.toString ()); 138 } 139 }