Let me give you the introduction of SOAP first. I know for many of you SOAP would be a new wold but I hope after reading this you will be quite familiar with SOAP.
SOAP defined as Simple Object Access Protocol, is aprotocol used for exchanging the information over the web to the web-service-enabled web site. Soap uses XML (eXtensible Markup Language) for sending message and also it receives response in the form of XML so it is very easy for any other website or 3rd party application to integrate with it.
Now I am not going into much detail of SOAP and its architecture but directly coming to the point where how we can actually send SOAP request and how we can receive SOAP response using the Java programming language.
Before we start doing actual implementation in Java we should know few things which are very important and necessary for sending the request and receiving the SOAP response. Those necessary things are – WSDL (Web Services Description Language), SOAP Message.
WSDL; there will be one URL where our WSDL is defined/stored on the server. It is an XML-based language that provides a model for describing Web services.
SOAP Message; in a simple language we can say a valid XML file which contains all input require getting the expected Response from the Server.
In java to send SOAP message to the server you need to import following classes.
These are the Classes use for sending and receiving Soap message.
These are the classes use for some file and stream operation.
Once you imports all above classes, create one Class of your choice and in main() function of your class start writing following statements one by one.
1. Create an Object of URL (here URL refers to the WSDL).
URL url = new URL(“YOUR WSDL URL GOES HERE”);
2. Now we have to establish connection using the above URL so that we can send SOAP Message to the server using the above WSDL.
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
3. Read Soap request (Soap message) XML byte by byte using “ByteArrayOutputStream()” and read XML file using “FileInputStream()” methods and store it in as a String in one variable.
String soapMessage = getSoapRequestXMLString();
getSoapRequestXMLString() is a function which will perform the operation mentioned in step 3 and it will return String of Soap Request XML.
4. Send message to the Soap Server.
We have to set some Properties of the Soap Message before we actually send soap messae. Those properties can be set using below statements.
Now to send actual Soap message which we have read it in one String (step 3), below line of code will help you to send message.
PrintWriter pw = new PrintWriter(connection.getOutputStream());
soapMessagecontains SOAP request XML (Step 3).
5. Once you have sent message to the server we have to connect again to receive Response. To receive response, use below lines of code.
Connect to the server and again read response byte by byte. You have to use same logic which you have used for reading XML request In step 3.
Here after connecting, use same connection (Step 2) for reading response bytes and save it as XML file.
You can play around with the code and add your own extra features like WS-Securities etc.
Please Note that above discussed method will not work if WS-security is enabled for that Web Service. I hope you will like this article.