1.5. Java HTTP post for XML SOAP message.

Something more basic and very low-level now: just post a SOAP message by writing the XML file yourself!

1.5.1. How to write a SOAP message

I am going to introduce briefly how to write a simple SOAP message to consume a remote procedure like: readLS(). Let's take a look at the xml below (rpc operation style with use of: encoded, so if you want to send a SOAP message to my on-line document/literal web service use Example 1-10):

You can find below a version, of the same SOAP message, for a document-style use of literal web service. And you can adapt easily for an other type of web service with the TcpTunnelGui tool (see here for installation and use).

Next section we will see how to upload an xml file to a web service. Pay attention at the rpc or document operation style of the web service you want to consume (see the WSDL) and use one of the two preceding SOAP message provided. The code below is for rpc/encoded or rpc/literal operation style.

1.5.2. Java XML HTTP post (document/literal)

Use the examples below as a template for posting an xml SOAP message to my document/literal operation style web service. Also, you can customize the message you send to the remote procedure (in bold).

Example 1-11. Upload the SOAP message: PostXml.java

import java.net.*;
import java.io.*;

public class PostXml {

  public static void main(String[] args) {
    try {
      String xmldata = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + 
	"<env:Envelope " + 
	  "env:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" " + 
	  "xmlns:env=\"http://schemas.xmlsoap.org/soap/envelope/\" " + 
	  "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " + 
	  "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
	  "<env:Header/>" +
          "<env:Body>" +
	    "<ans1:readLS xmlns:ans1=\"http://phonedirlux.homeip.net/types\">" +
	      "<String_1 xsi:type=\"xsd:string\">your message or e-mail</String_1>" +
	    "</ans1:readLS>" +
	  "</env:Body>" +
      //Create socket
      String hostname = "www.pascalbotte.be";
      int port = 80;
      InetAddress  addr = InetAddress.getByName(hostname);
      Socket sock = new Socket(addr, port);
      //Send header
      String path = "/rcx-ws/rcx";
      BufferedWriter  wr = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream(),"UTF-8"));
      // You can use "UTF8" for compatibility with the Microsoft virtual machine.
      wr.write("POST " + path + " HTTP/1.0\r\n");
      wr.write("Host: www.pascalbotte.be\r\n");
      wr.write("Content-Length: " + xmldata.length() + "\r\n");
      wr.write("Content-Type: text/xml; charset=\"utf-8\"\r\n");
      //Send data
      // Response
      BufferedReader rd = new BufferedReader(new InputStreamReader(sock.getInputStream()));
      String line;
      while((line = rd.readLine()) != null)
    } catch (Exception e) {

For more advanced sample you can use the tool: TcpTunnelGui to help you to format your SOAP message (see here for installation and use).

This technique should only be used for demo purpose or because you have to consume a SOAP web service and you don't want to be dependant of any library. Example: if you want to provide an applet on the internet available for every one. Check here if my applet is on-line. This, very basic, Java applet call the method status() and should work from any computer running Java.