KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlparser > http > HttpHeader


1 // HTMLParser Library - A java-based parser for HTML
2
// http://htmlparser.org
3
// Copyright (C) 2006 Derrick Oswald
4
//
5
// Revision Control Information
6
//
7
// $URL: https://svn.sourceforge.net/svnroot/htmlparser/trunk/parser/src/main/java/org/htmlparser/http/HttpHeader.java $
8
// $Author: derrickoswald $
9
// $Date: 2006-09-16 10:44:17 -0400 (Sat, 16 Sep 2006) $
10
// $Revision: 4 $
11
//
12
// This library is free software; you can redistribute it and/or
13
// modify it under the terms of the Common Public License; either
14
// version 1.0 of the License, or (at your option) any later version.
15
//
16
// This library is distributed in the hope that it will be useful,
17
// but WITHOUT ANY WARRANTY; without even the implied warranty of
18
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
// Common Public License for more details.
20
//
21
// You should have received a copy of the Common Public License
22
// along with this library; if not, the license is available from
23
// the Open Source Initiative (OSI) website:
24
// http://opensource.org/licenses/cpl1.0.php
25

26 package org.htmlparser.http;
27
28 import java.io.IOException JavaDoc;
29 import java.net.HttpURLConnection JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33
34 /**
35  * Utility methods to display HTTP headers.
36  */

37 public class HttpHeader
38 {
39     
40     /**
41      * Private constructor.
42      * This class is completely static.
43      */

44     private HttpHeader ()
45     {
46     }
47
48     /**
49      * Gets the request header for the connection.
50      * <em>This header is generated from the contents of the connection
51      * and may not be exactly the same as the request that will be sent.</em>
52      * @param connection The connection to convert into an HTTP request header.
53      * @return The string that would be sent by the HTTP request.
54      */

55     public static String JavaDoc getRequestHeader (HttpURLConnection JavaDoc connection)
56     {
57         // dump it
58
StringBuffer JavaDoc buffer;
59         Map JavaDoc map;
60         String JavaDoc key;
61         List JavaDoc items;
62
63         buffer = new StringBuffer JavaDoc (1024);
64         buffer.append (connection.getRequestMethod ());
65         buffer.append (" ");
66         buffer.append (connection.getURL ());
67         buffer.append (" HTTP/1.1\n");
68         map = connection.getRequestProperties ();
69         for (Iterator JavaDoc iter = map.keySet ().iterator (); iter.hasNext (); )
70         {
71             key = (String JavaDoc)iter.next ();
72             items = (List JavaDoc)map.get (key);
73             buffer.append (key);
74             buffer.append (": ");
75             for (int i = 0; i < items.size (); i++)
76             {
77                 if (0 != i)
78                     buffer.append (", ");
79                 buffer.append (items.get (i));
80             }
81             buffer.append ("\n");
82         }
83
84         return (buffer.toString ());
85     }
86
87     /**
88      * Gets the response header for the connection.
89      * Calling this method on an un-connected connection will
90      * generate an error, as will an attempt to get information
91      * from a connected but invalid connection.
92      * <em>This header is generated from the contents of the connection
93      * and may not be exactly the same as the response that was received.</em>
94      * @param conn The connection to convert into an HTTP response header.
95      * @return The string that was sent as the HTTP response.
96      */

97     public static String JavaDoc getResponseHeader (HttpURLConnection JavaDoc conn)
98     {
99         // dump it
100
StringBuffer JavaDoc buffer;
101         int code;
102         String JavaDoc message;
103         String JavaDoc key;
104         String JavaDoc value;
105
106         buffer = new StringBuffer JavaDoc (1024);
107         try
108         {
109             code = conn.getResponseCode ();
110             if (-1 != code)
111             {
112                 message = conn.getResponseMessage ();
113                 for (int i = 0; null != (value = conn.getHeaderField (i)); i++)
114                 {
115                     key = conn.getHeaderFieldKey (i);
116                     if ((null == key) && (0 == i))
117                     {
118                         buffer.append ("HTTP/1.1 ");
119                         buffer.append (code);
120                         buffer.append (" ");
121                         buffer.append (message);
122                         buffer.append ("\n");
123                     }
124                     else
125                     {
126                         if (null != key)
127                         {
128                             buffer.append (key);
129                             buffer.append (": ");
130                         }
131                         buffer.append (value);
132                         buffer.append ("\n");
133                     }
134                 }
135             }
136         }
137         catch (IOException JavaDoc ioe)
138         {
139             buffer.append (ioe.toString ());
140         }
141
142         return (buffer.toString ());
143     }
144 }
145
Popular Tags