SOAP Message Using Sockets

Recently i had to call a webservice for creation of a demo. And i had no time to learn any packages which create SOAP envelope’s. The only thing i know is plan old java. Then i thought why not use sockets for sending messages. Here is a sample Program which sends soap messages and gets response using sockets in Java

 * To change this template, choose Tools | Templates
 * and open the template in the editor.
package com;

 * @author Ashwin
public class PostXml {

    public static void main(String[] args) {

        try {
            String xmldata = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><soapenv:Envelope xsi:schemaLocation=\"\" xmlns:xsi = \"\" xmlns:xsd = \"\" xmlns:soapenv = \"\" xmlns:tab = \"\" > <soapenv:Body><tab:PURCHASE_ORDER> <tab:PURCHASE_ORDER_Record><tab:CUSTOMER_ID>1</tab:CUSTOMER_ID><tab:ORDER_NUM>14</tab:ORDER_NUM><tab:QUANTITY>1</tab:QUANTITY><tab:SHIPPING_DATE>2006-07-28</tab:SHIPPING_DATE><tab:FREIGHT_COMPANY>PRimeSOft</tab:FREIGHT_COMPANY><tab:SALES_DATE>2006-07-26</tab:SALES_DATE><tab:SHIPPING_COST>901</tab:SHIPPING_COST><tab:PRODUCT_ID>980001</tab:PRODUCT_ID></tab:PURCHASE_ORDER_Record></tab:PURCHASE_ORDER></soapenv:Body></soapenv:Envelope>";

            //Create socket
            String hostname = "localhost";
            int port = 11080;
            InetAddress addr = InetAddress.getByName(hostname);
            Socket sock = new Socket(addr, port);

            //Send header
            String path = "/PlaceOrderService/PlaceOrderPort";
            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: localhost\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) {
                System.out.println("Response :"+line);
        } catch (Exception e) {

:- More to Continue On Webservices


7 thoughts on “SOAP Message Using Sockets

  1. Hi, thank you for this add.
    But i think that the soap message should be sent in a HTTP request ,so the port number will be 80 .Do you have any idea about this ?

  2. If we type some URL as this is similar as typing

    Browser submit all http request by default on port 80. Incase you want to host your site one some other port you can still host a http server on that port and use it.

    Note: Most of the company firewalls disallow ports other than 80 to be exposed outside organization so you may not be able to use it in your company

  3. hello!,I love your writing so so much! share we be in contact more about your post on AOL? I need an expert on this house to solve my problem. May be that’s you! Looking forward to peer you.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s