pratique :

envois de message Soap à des services
dont on connait la signature WSDL

 

1. Récupérez le package et compilez le programme de construction de requete HTTP pour Soap

Chez http://www.innovation.ch/java/HTTPClient/ récupérez la librairie de client http pour java,
et compilez :

import HTTPClient.*;
import java.io.*;
import java.net.*;

class SoapHttpConnect {
  public static void main(String arg[]) {
    if (arg.length != 3)  {
      System.out.println(" erreur : mauvais nombre d'argument");
      System.out.println(" usage : SoapHttpConnect URL UrnSOAPAction SoapContentXMLFile");
      System.exit(1);
    }
    String  soapFile = arg[2];
    StringBuffer soapContent=new  StringBuffer();
    String hostUrl = "", fileUrl = "";
    try { // récupérer le host et le fichier de l'URl ciblée
      URL url ; 
      if (arg[0].startsWith("http://"))
        url = new URL(arg[0]);
      else
        url = new URL("http://" +arg[0]);
      hostUrl = url.getHost();
      fileUrl = url.getFile();
    } catch (MalformedURLException  e) {
      System.out.println(" erreur :" + e.toString());
      System.exit(1);
    }
    String urnSOAPAction = new String("\""+arg[1]+"\"");
    try { // récupérer le contenu soap
      File fichier=new File(soapFile);
      FileReader flotLecture = new FileReader(fichier);
      long longueurFichier= fichier.length();
      int dejaLu = 0;
      char car=0;
      while (dejaLu < longueurFichier) {
        car= (char)flotLecture.read();
        dejaLu = dejaLu + 1;
        soapContent.append(car);
      }
      flotLecture.close();
    } catch (IOException e) {
      System.out.println(" erreur :" + e.toString());
      System.exit(2);
    }
    try {
      NVPair pairHeader[] = new NVPair[3]; // former l'entete HTTP
      pairHeader[0] = new NVPair("Content-Type", "text/xml; charset=utf-8");
      pairHeader[1] = new NVPair("Content-Length", 
                                  String.valueOf(soapContent.length()));
      pairHeader[2] = new NVPair("SOAPAction", urnSOAPAction);
      HTTPConnection connect = new HTTPConnection(hostUrl);
      // envoyer la requete POST
      HTTPResponse reponse = connect.Post(fileUrl, 
                                     soapContent.toString(), pairHeader);
      if (reponse.getStatusCode() >= 300)  {
        System.err.println("Received Error: "+reponse.getReasonLine());
        System.err.println(reponse.getText());
        System.exit(3);
      } else {
        String textReponse = reponse.getText();
        System.out.println(" réponse :") ;
        System.out.println( textReponse) ;
      }
    } catch (IOException ioe) {
      System.err.println(ioe.toString());
      System.exit(4);
    } catch (ParseException pe) {
      System.err.println("Error parsing Content-Type: " + pe.toString());
      System.exit(5);
    } catch (ModuleException me) {
      System.err.println("Error handling request: " + me.getMessage());
      System.exit(6);
    }
  }
}

2. Analysez la description WSDL d'un service SOAP

Romulan Numbers XLII :
trouvé sur le site des Web services : xmethods

<?xml version="1.0" encoding="utf-8"?>
<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" 
   xmlns:xs="http://www.w3.org/2001/XMLSchema" 
   name="IRomanservice" targetNamespace="http://eBob42.org/" 
   xmlns:tns="http://eBob42.org/" 
   xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
   xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" 
   xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/">
  <message name="IntToRoman0Request">
    <part name="Int" type="xs:int"/>
  </message>
  <message name="IntToRoman0Response">
    <part name="return" type="xs:string"/>
  </message>
  <message name="RomanToInt1Request">
    <part name="Rom" type="xs:string"/>
  </message>
  <message name="RomanToInt1Response">
    <part name="return" type="xs:int"/>
  </message>
  <portType name="IRoman">
    <operation name="IntToRoman">
      <input message="tns:IntToRoman0Request"/>
      <output message="tns:IntToRoman0Response"/>
    </operation>
    <operation name="RomanToInt">
      <input message="tns:RomanToInt1Request"/>
      <output message="tns:RomanToInt1Response"/>
    </operation>
  </portType>
  <binding name="IRomanbinding" type="tns:IRoman">
    <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
    <operation name="IntToRoman">
      <soap:operation soapAction="urn:Roman-IRoman#IntToRoman" style="rpc"/>
      <input message="tns:IntToRoman0Request">
        <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:Roman-IRoman"/>
      </input>
      <output message="tns:IntToRoman0Response">
        <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:Roman-IRoman"/>
      </output>
    </operation>
    <operation name="RomanToInt">
      <soap:operation soapAction="urn:Roman-IRoman#RomanToInt" style="rpc"/>
      <input message="tns:RomanToInt1Request">
        <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:Roman-IRoman"/>
      </input>
      <output message="tns:RomanToInt1Response">
        <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:Roman-IRoman"/>
      </output>
    </operation>
  </binding>
  <service name="IRomanservice">
    <port name="IRomanPort" binding="tns:IRomanbinding">
      <soap:address location="http://www.ebob42.com/cgi-bin/Romulan.exe/soap/IRoman"/>
    </port>
  </service>
</definitions>
  

Ce qui signifie :


  

3. Ecrire l'enveloppe SOAP et lancer la requête

dans un fichier request.xml :

<soap:Envelope
    xmlns:mrns0="urn:Roman-IRoman"
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soap:Body soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
    <mrns0:IntToRoman>
       <Int xsi:type="xs:int">13</Int>
      </mrns0:IntToRoman>
   </soap:Body>
</soap:Envelope>

Puis sur la console :

$ CLASSPATH=. ; export CLASSPATH
$ java SoapHttpConnect http://www.ebob42.com/cgi-bin/Romulan.exe/soap/IRoman
               urn:Roman-IRoman#IntToRoman request.xml
 réponse :
<?xml version="1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
  <SOAP-ENV:Body SOAP-ENC:encodingStyle="http://schemas.xmlsoap.org/soap/envelope/">
    <NS1:IntToRomanResponse xmlns:NS1="urn:Roman-IRoman">
      <return xsi:type="xsd:string">
        XIII
      </return>
    </NS1:IntToRomanResponse>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
  

4. Exercice :

Construire et envoyer quelques requetes.
En allant sur le site des web services xmethods


[page suivante] [index]