1 26 package org.htmlparser.http; 27 28 import java.io.IOException ; 29 import java.net.HttpURLConnection ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Map ; 33 34 37 public class HttpHeader 38 { 39 40 44 private HttpHeader () 45 { 46 } 47 48 55 public static String getRequestHeader (HttpURLConnection connection) 56 { 57 StringBuffer buffer; 59 Map map; 60 String key; 61 List items; 62 63 buffer = new StringBuffer (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 iter = map.keySet ().iterator (); iter.hasNext (); ) 70 { 71 key = (String )iter.next (); 72 items = (List )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 97 public static String getResponseHeader (HttpURLConnection conn) 98 { 99 StringBuffer buffer; 101 int code; 102 String message; 103 String key; 104 String value; 105 106 buffer = new StringBuffer (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 ioe) 138 { 139 buffer.append (ioe.toString ()); 140 } 141 142 return (buffer.toString ()); 143 } 144 } 145 | Popular Tags |