1 5 package com.oreilly.servlet; 6 7 import java.io.*; 8 import java.net.*; 9 import java.util.*; 10 11 import com.knowgate.misc.Base64Encoder; 12 13 46 public class HttpMessage { 47 48 URL servlet = null; 49 Hashtable headers = null; 50 51 58 public HttpMessage(URL servlet) { 59 this.servlet = servlet; 60 } 61 62 68 public InputStream sendGetMessage() throws IOException { 69 return sendGetMessage(null); 70 } 71 72 80 public InputStream sendGetMessage(Properties args) throws IOException { 81 String argString = ""; 83 if (args != null) { 84 argString = "?" + toEncodedString(args); 85 } 86 URL url = new URL(servlet.toExternalForm() + argString); 87 88 URLConnection con = url.openConnection(); 90 con.setUseCaches(false); 91 92 sendHeaders(con); 94 95 return con.getInputStream(); 96 } 97 98 104 public InputStream sendPostMessage() throws IOException { 105 return sendPostMessage(null); 106 } 107 108 116 public InputStream sendPostMessage(Properties args) throws IOException { 117 String argString = ""; if (args != null) { 119 argString = toEncodedString(args); } 121 122 URLConnection con = servlet.openConnection(); 123 124 con.setDoInput(true); 126 con.setDoOutput(true); 127 128 con.setUseCaches(false); 130 131 con.setRequestProperty("Content-Type", 133 "application/x-www-form-urlencoded"); 134 135 sendHeaders(con); 137 138 DataOutputStream out = new DataOutputStream(con.getOutputStream()); 140 out.writeBytes(argString); 141 out.flush(); 142 out.close(); 143 144 return con.getInputStream(); 145 } 146 147 163 public InputStream sendPostMessage(Serializable obj) throws IOException { 164 URLConnection con = servlet.openConnection(); 165 166 con.setDoInput(true); 168 con.setDoOutput(true); 169 170 con.setUseCaches(false); 172 173 con.setRequestProperty("Content-Type", 175 "application/x-java-serialized-object"); 176 177 sendHeaders(con); 179 180 ObjectOutputStream out = new ObjectOutputStream(con.getOutputStream()); 182 out.writeObject(obj); 183 out.flush(); 184 out.close(); 185 186 return con.getInputStream(); 187 } 188 189 197 public void setHeader(String name, String value) { 198 if (headers == null) { 199 headers = new Hashtable(); 200 } 201 headers.put(name, value); 202 } 203 204 private void sendHeaders(URLConnection con) { 206 if (headers != null) { 207 Enumeration headerenum = headers.keys(); 208 while (headerenum.hasMoreElements()) { 209 String name = (String ) headerenum.nextElement(); 210 String value = (String ) headers.get(name); 211 con.setRequestProperty(name, value); 212 } 213 } 214 } 215 216 224 public void setCookie(String name, String value) { 225 if (headers == null) { 226 headers = new Hashtable(); 227 } 228 String existingCookies = (String ) headers.get("Cookie"); 229 if (existingCookies == null) { 230 setHeader("Cookie", name + "=" + value); 231 } 232 else { 233 setHeader("Cookie", existingCookies + "; " + name + "=" + value); 234 } 235 } 236 237 245 public void setAuthorization(String name, String password) { 246 String authorization = Base64Encoder.encode(name + ":" + password); 247 setHeader("Authorization", "Basic " + authorization); 248 } 249 250 253 private String toEncodedString(Properties args) { 254 StringBuffer buf = new StringBuffer (); 255 Enumeration names = args.propertyNames(); 256 while (names.hasMoreElements()) { 257 String name = (String ) names.nextElement(); 258 String value = args.getProperty(name); 259 buf.append(URLEncoder.encode(name) + "=" + URLEncoder.encode(value)); 260 if (names.hasMoreElements()) buf.append("&"); 261 } 262 return buf.toString(); 263 } 264 } 265 | Popular Tags |