// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.net.*;
import java.io.*;

public class WSTest {
   public String marshall() {
      StringBuffer payload = new StringBuffer("<?xml version='1.0' encoding='UTF-8'?>");
      payload.append("<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>\r\n"
            + "<soap:Body>\r\n"
            + "<test xmlns='http://localhost:8080/axis/Test.jws'>\r\n"
            + "</test>\r\n"
            + "</soap:Body>\r\n"
            + "</soap:Envelope>\r\n");
      return payload.toString();
   }
   public void post() {
      try {
         String request = marshall();
         URL url = new URL("http://localhost:8082/axis/Test.jws");
         HttpURLConnection con = (HttpURLConnection) url.openConnection();
         con.setRequestMethod("POST");
         con.setDoInput(true);
         con.setDoOutput(true);
         con.setUseCaches(false);
         con.setAllowUserInteraction(false);
         con.setRequestProperty("Content-Length", Integer.toString(request.length()));
         con.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
         con.setRequestProperty("SOAPAction", "Test.jws");
         OutputStream out = con.getOutputStream();
         out.write(request.getBytes());
         out.flush();
         out.close();
         System.out.println(con.getResponseCode() + " - " + con.getResponseMessage());
         InputStream in = con.getInputStream();
         byte[] b = new byte[1000];
         int n;
         while((n = in.read(b)) >= 0) {
            System.out.println(new String(b,0,n));
         }
      } catch (MalformedURLException e1) {
         System.out.println(e1.getMessage());
      } catch (IOException e2) {
         System.out.println(e2.getMessage());
      }
   }
   public static void main(String[] args) {
      WSTest sc = new WSTest();
      sc.post();
   }
}
