You are viewing a plain text version of this content. The canonical link for it is here.
Posted to soap-dev@xml.apache.org by Ed Keen <ed...@interactiveportal.com> on 2001/03/16 14:06:42 UTC

code for Microsoft server/Apache client

Here is my code for a test Microsoft soap server program.  First, you have
to follow these steps (I have taken these steps straight from Microsoft's
SOAP Toolkit 2.0 Beta 2 help file):

1.  Open VB and create an ActiveX DLL project.
2.  Change the Project Name to "SoapSample"  (you must do this in 2 places
-- the actual name and in the "properties")
3.  Add references to 
	* Microsoft XML, v3.0
	* Microsoft SOAP Type Library (Ver 1.0)
	* Microsoft Active Server Pages Object Library 

4.  Rename Class1 to "TestSoap"  (you must do this in 2 places -- the actual
name in in the "properties")
5.  Add the code for TestSoap.cls (code listing below)
6.  On the File menu, click Make SoapSample.dll

Now, you must create an Active Server Page to be the listener (you can also
write an ISAPI plugin, but I didn't want to taint myself too much with
Microsoft -- just wanted to prove the concept).  Here are the steps for
that:

1.  On the server running IIS, create a virtual root folder and name it
SoapSample. 

2.  In the folder referenced by the virtual folder, create a file, save the
following code to this file, and then save the file with the name,
SoapSample.asp. 

<%
Set TestSoap = Server.CreateObject("SoapSample.TestSoap")
TestSoap .Process Request, Response
%>  



Here is the code for TestSoap.cls:

------------------------------------

Option Explicit

Const NS_URI_SOAP_ENC = "http://schemas.xmlsoap.org/soap/encoding/"
Const TESTSOAP_NS = "urn:TestSoap"


Public Sub Process(ByVal Request As ASPTypeLibrary.Request, _
                   ByVal Response As ASPTypeLibrary.Response)

    Dim A As Double
    Dim B As Double
    Dim RetVal As String
    
    Dim Serializer As MSSOAPLib.SoapSerializer
    Dim Reader As MSSOAPLib.SoapReader
    Dim MethodName As String
    Dim param As String
    
    
    On Error Resume Next
    
    Set Serializer = New MSSOAPLib.SoapSerializer
    If Err Then
        ServerFault Response, _
                    "Cannot create MSSOAP.SoapSerializer. " & _
                    Err.Description & "(0x" & Hex(Err.Number) & ")"
        Exit Sub
    End If
    
    Set Reader = New MSSOAPLib.SoapReader
    If Err Then
        ServerFault Response, _
                    "Cannot create MSSOAP.SoapReader. " & _
                    Err.Description & "(0x" & Hex(Err.Number) & ")"
        Exit Sub
    End If
    
    Reader.Load Request
    If Err Then
        ClientFault Response, "Cannot load request. " & Err.Description
        Exit Sub
    End If
    
    MethodName = Reader.RPCStruct.baseName
    If Err Then
        ClientFault Response, _
                    "Cannot get method name. " & _
                    Err.Description
        Exit Sub
    End If
    
    
    Select Case MethodName
    
        Case "testMethod1"
            RetVal = "testMethod1 was called"
        
        Case "testMethod2"
            param = Reader.RPCParameter("param").Text
            
            If Err Then
                ClientFault Response, "Cannot get parameter 'param'. " &
Err.Description
                Exit Sub
            End If
            
            RetVal = "testMethod2 was called.  You passed '" & param & "'"
        
        Case Else
            ClientFault Response, "Unknown method: '" & MethodName & "'."
            Exit Sub
        
    End Select
    
    
    Response.ContentType = "text/xml"
    
    Serializer.Init Response
    
    Serializer.startEnvelope
    Serializer.SoapAttribute "xmlns:xsi", ,
"http://www.w3.org/1999/XMLSchema-instance"
    Serializer.SoapAttribute "xmlns:xsd", ,
"http://www.w3.org/1999/XMLSchema"
    
        Serializer.startBody
            Serializer.startElement MethodName & "Response", TESTSOAP_NS,
NS_URI_SOAP_ENC, "ns1"
                Serializer.startElement "return", TESTSOAP_NS
                Serializer.SoapAttribute "xsi:type", , "xsd:string"
                    Serializer.writeString RetVal
                Serializer.endElement
            Serializer.endElement
        Serializer.endBody
    Serializer.endEnvelope
    
End Sub

Sub ServerFault(ByVal Response As ASPTypeLibrary.Response, _
                ByVal FaultString As String)
  ReturnFault Response, "Server", FaultString
End Sub

Sub ClientFault(ByVal Response As ASPTypeLibrary.Response, _
                ByVal FaultString As String)
  ReturnFault Response, "Client", FaultString
End Sub

Sub ReturnFault(ByVal Response As ASPTypeLibrary.Response, _
                ByVal FaultCode As String, _
                ByVal FaultString As String)

  On Error Resume Next
  
  Err.Clear

  Dim Serializer As MSSOAPLib.SoapSerializer

  Response.Status = "500 Internal Server Error"
  
  Set Serializer = New MSSOAPLib.SoapSerializer
  If Err Then

    Response.AppendToLog _
"Could not create SoapSerializer object. " & _
Err.Description
      
  Else

      Serializer.Init Response

      Serializer.startEnvelope
      Serializer.startBody
      Serializer.startFault FaultCode, FaultString
      Serializer.startFaultDetail
      Serializer.endFaultDetail
      Serializer.endFault
      Serializer.endBody
      Serializer.endEnvelope
      
  End If
  
End Sub

------------------------------------

Here is the code for my Apache soap client class:

package tests.soap;

import java.io.*;
import java.util.*;
import java.net.*;
import org.w3c.dom.*;
import org.apache.soap.util.xml.*;
import org.apache.soap.*;
import org.apache.soap.encoding.*;
import org.apache.soap.encoding.soapenc.*;
import org.apache.soap.rpc.*;

public class SoapClient
{
  public static void main(String[] args) throws Exception 
  {

    URL url = new URL("http://localhost/SoapSample/SoapSample.asp");

    Call call = new Call();
    call.setTargetObjectURI("urn:TestSoap");
    call.setMethodName("testMethod2");
    call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);

    Vector params = new Vector();

    params.addElement(new Parameter("param", String.class,
                                    "foo foo", null));

    call.setParams(params);
    Response resp;

    try
    {
      resp = call.invoke(url, "");
    }
    catch (SOAPException e)
    {
      System.err.println("Caught SOAPException (" +
                         e.getFaultCode() + "): " +
                         e.getMessage());
      return;
    }

    // Check the response.
    if (!resp.generatedFault())
    {
      Parameter ret = resp.getReturnValue();
      Object value = ret.getValue();

	  if (value == null)
		System.out.println("I don't know");
	  else
		System.out.println("\n" + value);
    }
    else
    {
      Fault fault = resp.getFault();

      System.err.println("Generated fault: ");
      System.out.println ("  Fault Code   = " + fault.getFaultCode());  
      System.out.println ("  Fault String = " + fault.getFaultString());
    }
  }