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 Ma...@GECITS-EU.COM on 2001/03/13 09:36:52 UTC

Apache Soap and Microsoft Soap

Hi!

I've trying to connect to a Microsoft SOAP service (V1.0) Server. I've
adpated an example provided by the Apache org - GuidGen...
Also I try to do this by other way (right or wrong, I don't know...).
Neither of those result on errors, which I can't explain!

What I want to do is, contact a SOAP service that receives a parameter and
that returns a simple result composed by that parameter...

The main idea is to develop Microsoft Soap Services accessible by all
platforms (Java, VB, C++,...)...

Anyone can help me out on this one, please??

Thanks a lot
Marco

Here's the source code:
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.*;
import org.apache.soap.transport.http.SOAPHTTPConnection;

import com.sun.xml.tree.*;
import org.w3c.dom.*;
import com.sun.xml.parser.*;

/**
 * Client to speak to MS SOAP implemented GUIDGen service at
 * http://www.itfinity.net/soap/guid/details.html
 *
 * @author Sanjiva Weerawarana (sanjiva@watson.ibm.com)
 */
public class SoapHelloWorld {

     public static void main(String[] args) throws Exception
     {
     boolean passagemDirecta=true;
          if(passagemDirecta)
          {
               String nome="Milton";
               String envelope="<?xml version='1.0'?>"+
                         "<SOAP-ENV:Envelope xmlns:SOAP-ENV
='http://schemas.xmlsoap.org/soap/envelope/' SOAP-ENV:encodingStyle
='http://schemas.xmlsoap.org/soap/encoding/' xmlns:xsi
='http://www.w3.org/1999/XMLSchema-instance' xmlns:xsd
='http://www.w3.org/1999/XMLSchema'>"+
                         "<SOAP-ENV:Body>"+
                         "<HelloWorld>"+
                         "<name>"+nome+"</name>"+
                         "</HelloWorld>"+
                         "</SOAP-ENV:Body>"+
                         "</SOAP-ENV:Envelope>";
     //System.out.println(envelope);
               URL url=new URL
("http","193.102.124.58",80,"/HelloWorld/HelloWorld.asp");
               //URL url=new URL
("http","193.102.124.58",80,"/hello/index2.asp");


               URLConnection uc=url.openConnection();
               uc.setDoOutput(true);
               uc.setDoInput(true);

               PrintWriter pout=new PrintWriter(uc.getOutputStream());
               pout.println(URLEncoder.encode(envelope));
               //pout.println("nome="+ URLEncoder.encode("marco"));
               pout.close();

     System.out.println(uc.getContentType());

               //Apenas consegui ler ficheiros .xml
               //XmlDocument xml=XmlDocument.createXmlDocument( "
http://193.102.124.58:80/HelloWorld/HelloWorld.asp",false);
               //System.out.println(getResult(xml));

               //BufferedReader i =new BufferedReader( new
InputStreamReader(url.openStream()) );
               BufferedReader i =new BufferedReader( new
InputStreamReader(uc.getInputStream()) );
               do{
                    System.out.println(i.readLine());
               }while(i.readLine()!=null);
               i.close();
          }
          else
          {
               String serviceURL = null;
               String proxyHost = null;
               String name="Marco";
               int proxyPort = -1;

               serviceURL = "
http://193.102.124.58/HelloWorld/HelloWorld.asp";
               //serviceURL = "
http://193.102.124.58/HelloWorld/HelloWorld.xml";

     System.out.println("O servico URL: " + serviceURL);
               URL url = new URL (serviceURL);

               // define deserializers for the return things (without
xsi:type)
               SOAPMappingRegistry smr = new SOAPMappingRegistry ();
               StringDeserializer sd = new StringDeserializer ();
               smr.mapTypes (Constants.NS_URI_SOAP_ENC,new QName ("",
"result"), null, null, sd);
     System.out.println("Passou 1...");

               // create the transport and set parameters
               SOAPHTTPConnection st = new SOAPHTTPConnection();
               proxyHost="193.102.124.58";
               proxyPort=80;
               if (proxyHost != null) {
                    st.setProxyHost(proxyHost);
                       st.setProxyPort(proxyPort);
               }
     System.out.println("Passou 2...");

               // build the call.
               Call call = new Call ();
               call.setSOAPTransport(st);
               call.setSOAPMappingRegistry (smr);
               call.setTargetObjectURI ("
http://193.102.124.58/HelloWorld/HelloWorld.xml");
               call.setMethodName("HelloWorld");
               call.setEncodingStyleURI ("
http://schemas.xmlsoap.org/soap/encoding/");
               //call.setEncodingStyleURI (Constants.NS_URI_SOAP_ENC);
     System.out.println("Passou 3...");

                //Set parameters
               Vector params = new Vector();

               params.addElement(new Parameter("name", String.class,name,
null));
               call.setParams(params);

     System.out.println("Passou 4...");

               // invoke it
               System.err.println ("Invoking GUID generator service at: ");
               System.err.println ("\t" + serviceURL);
               Response resp;
               try {
     System.out.println("Vai invocar servico.........");
                     //XMLDocument
xml=XMLDocument.createXmlDocument(call.invoke (url, serviceURL),false);
                     resp = call.invoke (url, serviceURL);
               } catch (SOAPException e) {
                         System.err.println("Caught SOAPException (" +
e.getFaultCode () + "): " + e.getMessage ());
                         return;
                   }

     System.out.println("Passou 5...");
               // check response
               if (!resp.generatedFault()) {
                     Parameter ret = resp.getReturnValue();
                     Object value = ret.getValue();

                     System.out.println ("Resultado obtido: " + 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());
               }
          }
     }

     public static String getAtributo(XmlDocument c)
     {
          String m="",cp="";
          ElementNode rootElement;
          NodeList  roteiroList;
          Node roteiro;

          try
          {
               rootElement = (ElementNode) c.getDocumentElement ();
               roteiroList = rootElement.getElementsByTagName("soap");

               for(int i=0;i < roteiroList.getLength();i++)
               {
                    roteiro = roteiroList.item(i);
                    Element e;
                          e=(Element) roteiro;
                          m=e.getAttribute("xmlns");
               }
          }catch(Exception e)
          {
              System.out.println("Erro: " + e.getMessage());
              System.exit(1);
          }

          return m;
     }

     public static String getResult(XmlDocument c)
     {
          String m="",cp="";
          ElementNode rootElement;
          NodeList  roteiroList;
          Node roteiro;

          try
          {
               rootElement = (ElementNode) c.getDocumentElement ();
               roteiroList = rootElement.getElementsByTagName("result");

               for(int i=0;i < roteiroList.getLength();i++)
               {
                    roteiro = roteiroList.item(i);
                    m=extractValue(roteiro).trim();
               }
          }catch(Exception e)
          {
              System.out.println("Erro: " + e.getMessage());
              System.exit(1);
          }

          return m;
     }

     private static String extractValue(Node node)
     {
          String value = "";
          NodeList children = node.getChildNodes();
          for(int i = 0; i < children.getLength(); i++ )
          {
              Node ci = children.item(i);
              if( ci.getNodeType() == Node.TEXT_NODE )
              {
                  value = value + ci.getNodeValue();
              }
          }
          return value;
     }

}