public static int GET = 0;
public
static int POST = 1;
private URL servlet;
public ServletMessage(URL servlet) {
this.servlet =
servlet;
System.out.println(servlet.toString());
}
public InputStream
sendMessage(Properties args) throws IOException
{
return sendMessage(args,
POST);
}
public InputStream
sendMessage(Properties args, int method)
throws IOException {
if (method == GET) {
URL url = new
URL(servlet.toExternalForm() + "?"
+
toEncodedString(args));
return
url.openStream();
} else {
URLConnection conn =
servlet.openConnection();
conn.setDoOutput(true);
conn.setUseCaches(false);
// POST the request data (html form
encoded)
PrintStream out = new
PrintStream(conn.getOutputStream());
if (args
!= null && args.size() > 0)
{
System.out.println("POST
"+toEncodedString(args));
out.print(toEncodedString(args));
}
out.close();
// ESSENTIAL for this to work!
// Read the POST response data
return
conn.getInputStream();
}
}
public String
toEncodedString(Properties args) {
StringBuffer sb = new StringBuffer();
if
(args != null) {
String sep =
"";
Enumeration names =
args.propertyNames();
while
(names.hasMoreElements()) {
String name =
(String)names.nextElement();
System.out.println(name);
sb.append(sep
+ URLEncoder.encode(name) + "="
+
URLEncoder.encode(args.getProperty(name)));
System.out.println(sep
+ URLEncoder.encode(name) + "="
+
URLEncoder.encode(args.getProperty(name)));
sep
=
"&";
}
}
System.out.println(sb.toString());
return
sb.toString();
}