BizTalk Utilities CV ,   Jobs ,   Code library
 
Home Page


Add/Edit your code items
Search the code library
Browse for the code library


WCF, WS, SOAP
Calling an Web service from a Managed C++ project
How to access asyncronously from C# an web service
What is XML-RPC?
Web Service and DHTML
Use XMLDOM and XMLHTTP Documents to Upload Small Files
Temperature Conversion XML WebService
SOAP Client Over HTTP Using Visual C++
SOAP Toolset for VB 6.0
Remote Database Administer via XMLHTTP
Binding XML Data Island to Web Forms
Security Model for Web Services
What is a XML Web Service?


 
 

<< UncategorizedXALAN >>


By Prabu Ramalingam
First Posted 10/22/2001
Times viewed 3119

Getting started with SOAP


Summary This article introduces the readers to the world of SOAP, a XML way of distributed programming.

SOAP- Getting Started What is Soap?

SOAP is a protocol specification for invoking methods on servers, services, components and objects. SOAP codifies the existing practice of using XML and HTTP as a method invocation mechanism. The SOAP specification mandates a small number of HTTP headers that facilitate firewall/proxy filtering. The SOAP specification also mandates an XML vocabulary that is used for representing method parameters, return values, and exceptions. SOAP speaks of xml way of distributed programming.

Let us see an example

Now let us write a class in java and publish it as web service. And also we will call the web service methods from a Java Client and VB client which would show the proof of concept. 

Configuration

 

  1. Download and install Apache Tomcat (jakarta-tomcat-3.1.1.zip)&SOAP2.0
  2. Unzip the downloaded soap zip file:
    This results in the creation of a soap-2_0 subdirectory.
  3. Unzip the downloaded tomcat server and now you will see a folder named jakarta-tomcat.
  4. Add a new context to the jakarta-tomcat\conf\server.xml configuration file like this(at the end of the file): <Context path=/apache-soap docBase=E:/soap-2_0/webapps/soapdebug=1 reloadable=true> </Context>
  5. Set up your Web server classpath:
    Apache SOAP requires Apache Xerces (Java) version 1.1.2 or higher, which supports the DOM (Document Object Model) level 2 candidate recommendation that provides namespace support. Use version 1.2 of Xerces, available from Apache as Xerces-J-bin.1.2.0.zip. Unzipping this file will result in the creation of a xerces-1_2_0 subdirectory.You must configure your Web server to use xerces.jar (which is in the xerces-1_2_0 subdirectory)-- as opposed to any native library/jar that came with your server -- for all XML parsing.

    For example, Tomcat comes with an XML parser (jakarta-tomcat\lib\xml.jar) that has the DOM level 1 interfaces.
    Even if you put the xerces.jar in your classpath, any Java code running in Tomcat will find the wrong interfaces because the batch file that you use to run Tomcat places your classpath at the end.
    Edit tomcat.bat (tomcat.sh for Unix) in the jakarta-tomcat\bin\directory
    and put xerces.jar at the front of the classpath that the script builds.
    Edit jakarta-tomcat\bin\tomcat.bat file:
    set CLASSPATH=E:\xerces-1_2_0\xerces.jar;%CLASSPATH%;%cp%
    You must also set your Web server's classpath to use soap.jar from the soap-2_0\lib\ subdirectory.
    Start the tomcat server.
    Now test your installation by requesting http://localhost:8080/soap/index.html

 

The HelloWorld service

The HelloWorld service expects to obtain a person/user's name and returns a customized hello message to the caller. The code below shows the complete implementation of the HelloWorld service:

package hello; public class HelloServer { public String sayHelloTo(String name) { System.out.println(sayHelloTo(String name)); return Hello + name + , How are you doing?; } } Deploy the service

 

Let us deploy this class as web service and allow others to invoke this method across globe.This web service can be called from a java/vb/c/c++/any other language.Requesting this web service is a xml request which contains many xml headers.

Apache SOAP makes creating services extremely easy. Basically, a service consists of the business logic, which is code that you would write regardless of how you made the service available to the rest of the world.

Request http://localhost:8080/soap/admin/index.html 

  1. Click on deploy menu.In the deploy page fill in the following data.
  2. ID: The Web service identifier.In our example,set the ID of the service to be urn:demo:hello, which follows the uniform resource Naming convention recommended by http://www.ietf.org/rfc/rfc2141.txt ,where demo is a namespace and exchange is the service string.
  3. Scope:The activation mode for the service. Request means that a new instance of the service is created with every individual request. The other available
    modes are Session and Application.
  4. Methods:A white-space delimited list of the methods to expose in the Web service.In our example enter sayHelloTo in the textfield.If you have two or more enter the methods comma seperated.
  5. Provider: The type of the Web service. Apache/SOAP currently only supports Java classes and Bean scripts, but future versions will undoubtedly support EJBs and other popular component types.
  6. Provider Class/Static: The name of the Java class. This class must be available via your class path setting. If Static is true, the methods are assumed to be static on the Java class, otherwise they are processed as instance methods.

The other fields are related to proprietary add-ons to the Apache/SOAP platform and are beyond the scope of this article.

Press deploy. Now the Hello service is deployed.

 

Invoking the Web Service: Client: Java Client Code
import java.net.*;
import java.util.*;
import org.apache.soap.*; // Body, Envelope, Fault, Header
import org.apache.soap.rpc.*; // Call, Parameter, Response
public class Client
{
public static void main( String[] args ) throws Exception
{
URL url = new URL( http://localhost:8080/soap/servlet/rpcrouter);
String urn = urn:demo:hello; // service id
Call call = new Call(); // prepare the service invocation
call.setTargetObjectURI( urn );
call.setMethodName( getRate );
call.setEncodingStyleURI( Constants.NS_URI_SOAP_ENC );
Vector params = new Vector();
params.addElement( new Parameter( name, String.class, Prabu,null ) ); // parameter to the service method deployed.
call.setParams( params );
try
{
System.out.println( invoke service\n + URL= + url + \n URN = + urn );
Response response = call.invoke( url, ); // invoke the service
if( !response.generatedFault() )
{
Parameter result = response.getReturnValue(); // response was OK
System.out.println( Result= + result.getValue() );
}
else
{
Fault f = response.getFault(); // an error occurred
System.err.println( Fault= + f.getFaultCode() + , +
f.getFaultString() );
}
}
catch( SOAPException e ) // call could not be sent properly
{
System.err.println( SOAPException= + e.getFaultCode() + , +
e.getMessage() );
}
}
}
VB Client

 

Install the XML DLL or go to project/reference/select MX XML libarary

Dim objHTTP As New MSXML.XMLHTTPRequest
Dim objReturn As New MSXML.DOMDocument
' str Envelope -- the SOAP XML request.(In java this xml construction is taken care by the class Call)
strEnvelope = _

          <SOAP-ENV:Envelope xmlns:SOAP-ENV=http://schemas.xmlsoap.org/soap/envelope/
          xmlns:xsi=http://www.w3.org/1999/XMLSchema-instance
          xmlns:xsd=http://www.w3.org/1999/XMLSchema>\n
          & _
          <SOAP-ENV:Body> & _
          <ns1:sayHelloTo xmlns:ns1=urn:demo:hello
          SOAP-ENV:encodingStyle=http://schemas.xmlsoap.org/soap/encoding/>
          & _
          <name xsi:type=xsd:string>
          + strName + </cardno> & _
          </ns1:sayHelloTo> & _
          </SOAP-ENV:Body> & _
          </SOAP-ENV:Envelope>


objHTTP.open POST, http://son1084:8080/soap/servlet/rpcrouter, False
objHTTP.setRequestHeader Content-Type, text/xml
'Make the SOAP call
objHTTP.send strEnvelope
' Get the response and process which is an xml.
'Get the return envelope
strReturn = objHTTP.responseText
'Load the return envelope into a DOM.
objReturn.loadXML strReturn
'procedd with your xml process.

If you intend to see the SOAP XML request/response,

  1. Run the following command,
    java org.apache.soap.util.net.TcpTunnelGui 8070 localhost 8080
    This starts up an GUI (awt frame). Let it run.
  2. And also in the java client code change this line

    URL url = new URL( http://localhost:8080/soap/servlet/rpcrouter );
    to
    URL url = new URL( http://localhost:8070/soap/servlet/rpcrouter );

    And in the VB code ahnage the line

    objHttp.open POST, http://son1084:8080/soap/servlet/rpcrouter, False
    to
    objHttp.open POST, http://son1084:8070/soap/servlet/rpcrouter, False

  3. Save and compile.Now run the program to see the SOAP XML request/response to be visible in the GUI(awt frame).

    From Request/Response XML, you could learn how request is framed in XML
    and how server replies in XML.

If further clarification needed, please contact me at praboooysh@yahoo.com


Rate this article on a scale of 1 to 10 (0 votes, average 0)

Your vote :  

<< UncategorizedXALAN >>





Leave a comment for this article
Your name
Your email (optional)
Your comment
Optional: Upload an attachment
Enter the code shown:

 
 

    Email TopXML