Les services › Webservices › Outils de texte (SOAP, façon WCF)
Outils de texte (SOAP, façon WCF) :
SOAP 1.1 Site (démonstration) Définition WSDL lue le 18/11/2026 à 21:38
Démonstration d’un service SOAP comme ceux de WCF (.svc) : définition découpée en plusieurs fichiers, types complexes, liste et énumération.
https://koakdesign.info/ws_demo/textes.phpOutils de texte de démonstration KoakDesign (façon WCF .svc).
POSTAnalyserTexteCompte les caractères, mots et lignes, et donne les mots les plus fréquents.
SOAPAction : http://koakdesign.info/ws/textes/ITextes/AnalyserTexte
Entrée (AnalyserTexte)
| Champ | Type | |
|---|---|---|
texte | string | facultatif |
nombreMots | int | facultatif |
Sortie (AnalyserTexteResponse)
| Champ | Type | |
|---|---|---|
AnalyserTexteResult | objet | facultatif |
└Caracteres | int | facultatif |
└Lignes | int | facultatif |
└Mots | int | facultatif |
└MotsFrequents | objet | facultatif |
└MotFrequent | objet | facultatif · liste |
└Mot | string | facultatif |
└Nombre | int | facultatif |
Exemples XML
Requête
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns0="http://koakdesign.info/ws/textes">
<soap:Body>
<ns0:AnalyserTexte>
<!-- facultatif -->
<ns0:texte>?</ns0:texte>
<!-- facultatif -->
<ns0:nombreMots>0</ns0:nombreMots>
</ns0:AnalyserTexte>
</soap:Body>
</soap:Envelope>Réponse (forme renvoyée par le serveur)
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns0="http://koakdesign.info/ws/textes" xmlns:ns1="http://schemas.datacontract.org/2004/07/KoakDesign.Textes">
<soap:Body>
<ns0:AnalyserTexteResponse>
<!-- facultatif -->
<ns0:AnalyserTexteResult>
<!-- facultatif -->
<ns1:Caracteres>0</ns1:Caracteres>
<!-- facultatif -->
<ns1:Lignes>0</ns1:Lignes>
<!-- facultatif -->
<ns1:Mots>0</ns1:Mots>
<!-- facultatif -->
<ns1:MotsFrequents>
<!-- facultatif, répétable -->
<ns1:MotFrequent>
<!-- facultatif -->
<ns1:Mot>?</ns1:Mot>
<!-- facultatif -->
<ns1:Nombre>0</ns1:Nombre>
</ns1:MotFrequent>
</ns1:MotsFrequents>
</ns0:AnalyserTexteResult>
</ns0:AnalyserTexteResponse>
</soap:Body>
</soap:Envelope>Remplissez les champs puis envoyez : la requête part du site vers https://koakdesign.info/ws_demo/textes.php.
Un champ facultatif laissé vide n’est pas envoyé.
XML envoyé (modifiable à la main)
; =========================================================
; Appel de l'opération SOAP AnalyserTexte et lecture de la réponse
; PureBasic 6.x (avant 6.00 : InitNetwork() au démarrage)
; =========================================================
EnableExplicit
; ----- Types de la réponse -----
Structure MotFrequent
Mot.s
Nombre.l
EndStructure
Structure MotsFrequents
List MotFrequent.MotFrequent()
EndStructure
Structure AnalyserTexteResult
Caracteres.l
Lignes.l
Mots.l
MotsFrequents.MotsFrequents
EndStructure
Structure AnalyserTexteReponse
AnalyserTexteResult.AnalyserTexteResult
EndStructure
; ----- Lecture XML : outils communs -----
Procedure.s KD_Nom(*Noeud) ; nom sans préfixe : « a:Nom » -> « Nom »
Protected Nom.s = GetXMLNodeName(*Noeud)
ProcedureReturn StringField(Nom, CountString(Nom, ":") + 1, ":")
EndProcedure
Procedure KD_Enfant(*Parent, Nom.s) ; premier sous-élément de ce nom (0 si absent)
Protected *N = ChildXMLNode(*Parent)
While *N
If XMLNodeType(*N) = #PB_XML_Normal And KD_Nom(*N) = Nom
ProcedureReturn *N
EndIf
*N = NextXMLNode(*N)
Wend
ProcedureReturn 0
EndProcedure
Procedure KD_PremierElement(*Parent) ; premier sous-élément, quel que soit son nom
Protected *N = ChildXMLNode(*Parent)
While *N And XMLNodeType(*N) <> #PB_XML_Normal
*N = NextXMLNode(*N)
Wend
ProcedureReturn *N
EndProcedure
Procedure.s KD_Texte(*Parent, Nom.s) ; texte d'un sous-élément ("" si absent)
Protected *N = KD_Enfant(*Parent, Nom)
If *N
ProcedureReturn GetXMLNodeText(*N)
EndIf
ProcedureReturn ""
EndProcedure
; ----- Lecture de la réponse dans les structures -----
Procedure KD_Lire_MotFrequent(*Noeud, *R.MotFrequent)
Protected *E
*R\Mot = KD_Texte(*Noeud, "Mot")
*R\Nombre = Val(KD_Texte(*Noeud, "Nombre"))
EndProcedure
Procedure KD_Lire_MotsFrequents(*Noeud, *R.MotsFrequents)
Protected *E
*E = ChildXMLNode(*Noeud) ; liste « MotFrequent » : tous les éléments de ce nom
While *E
If XMLNodeType(*E) = #PB_XML_Normal And KD_Nom(*E) = "MotFrequent"
AddElement(*R\MotFrequent())
KD_Lire_MotFrequent(*E, @*R\MotFrequent())
EndIf
*E = NextXMLNode(*E)
Wend
EndProcedure
Procedure KD_Lire_AnalyserTexteResult(*Noeud, *R.AnalyserTexteResult)
Protected *E
*R\Caracteres = Val(KD_Texte(*Noeud, "Caracteres"))
*R\Lignes = Val(KD_Texte(*Noeud, "Lignes"))
*R\Mots = Val(KD_Texte(*Noeud, "Mots"))
*E = KD_Enfant(*Noeud, "MotsFrequents") : If *E : KD_Lire_MotsFrequents(*E, @*R\MotsFrequents) : EndIf
EndProcedure
Procedure KD_Lire_AnalyserTexteReponse(*Noeud, *R.AnalyserTexteReponse)
Protected *E
*E = KD_Enfant(*Noeud, "AnalyserTexteResult") : If *E : KD_Lire_AnalyserTexteResult(*E, @*R\AnalyserTexteResult) : EndIf
EndProcedure
; ----- Appel -----
Procedure.i KD_AnalyserTexte(Enveloppe.s, *R.AnalyserTexteReponse)
Protected Requete, Xml, *Corps, *Rep, Reponse.s, Ok = #False
NewMap Entetes.s()
Entetes("Content-Type") = "text/xml; charset=utf-8"
Entetes("SOAPAction") = ~"\"http://koakdesign.info/ws/textes/ITextes/AnalyserTexte\""
Requete = HTTPRequest(#PB_HTTP_Post, "https://koakdesign.info/ws_demo/textes.php", Enveloppe, 0, Entetes())
If Requete
Reponse = HTTPInfo(Requete, #PB_HTTP_Response)
FinishHTTP(Requete)
Xml = ParseXML(#PB_Any, Reponse)
If Xml And XMLStatus(Xml) = #PB_XML_Success
*Corps = KD_Enfant(MainXMLNode(Xml), "Body")
*Rep = 0 : If *Corps : *Rep = KD_PremierElement(*Corps) : EndIf
If *Rep And KD_Nom(*Rep) = "Fault"
Debug "Erreur SOAP : " + KD_Texte(*Rep, "faultstring")
ElseIf *Rep
KD_Lire_AnalyserTexteReponse(*Rep, *R)
Ok = #True
EndIf
FreeXML(Xml)
Else
Debug "Réponse illisible : " + Reponse
EndIf
EndIf
ProcedureReturn Ok
EndProcedure
; ----- Exemple -----
Define Enveloppe.s = ~"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns0=\"http://koakdesign.info/ws/textes\">\n <soap:Body>\n <ns0:AnalyserTexte>\n <!-- facultatif -->\n <ns0:texte>?</ns0:texte>\n <!-- facultatif -->\n <ns0:nombreMots>0</ns0:nombreMots>\n </ns0:AnalyserTexte>\n </soap:Body>\n</soap:Envelope>"
Define Resultat.AnalyserTexteReponse
If KD_AnalyserTexte(Enveloppe, @Resultat)
Debug "Caracteres = " + Str(Resultat\AnalyserTexteResult\Caracteres)
Debug "Lignes = " + Str(Resultat\AnalyserTexteResult\Lignes)
Debug "Mots = " + Str(Resultat\AnalyserTexteResult\Mots)
Debug "MotsFrequents = " + Resultat\AnalyserTexteResult\MotsFrequents
EndIf
// =========================================================
// Appel de l'opération SOAP AnalyserTexte et lecture de la réponse
// .NET 6 et plus (programme console, instructions de premier niveau)
// Variante : « dotnet-svcutil <adresse du WSDL> » génère un client complet.
// =========================================================
using System.Globalization;
using System.Net.Http.Headers;
using System.Text;
using System.Xml.Linq;
var enveloppe = @"<?xml version=""1.0"" encoding=""utf-8""?>
<soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:ns0=""http://koakdesign.info/ws/textes"">
<soap:Body>
<ns0:AnalyserTexte>
<!-- facultatif -->
<ns0:texte>?</ns0:texte>
<!-- facultatif -->
<ns0:nombreMots>0</ns0:nombreMots>
</ns0:AnalyserTexte>
</soap:Body>
</soap:Envelope>";
using var http = new HttpClient();
var requete = new HttpRequestMessage(HttpMethod.Post, "https://koakdesign.info/ws_demo/textes.php");
requete.Content = new StringContent(enveloppe, Encoding.UTF8, "text/xml");
requete.Headers.Add("SOAPAction", "\"http://koakdesign.info/ws/textes/ITextes/AnalyserTexte\"");
using var reponse = await http.SendAsync(requete);
var doc = XDocument.Parse(await reponse.Content.ReadAsStringAsync());
var corps = doc.Root!.Elements().First(x => x.Name.LocalName == "Body").Elements().First();
if (corps.Name.LocalName == "Fault")
{
Console.WriteLine("Erreur SOAP : " + Soap.Text(corps, "faultstring"));
return;
}
var resultat = AnalyserTexteReponse.FromXml(corps);
Console.WriteLine($"Caracteres = {resultat.AnalyserTexteResult?.Caracteres}");
Console.WriteLine($"Lignes = {resultat.AnalyserTexteResult?.Lignes}");
Console.WriteLine($"Mots = {resultat.AnalyserTexteResult?.Mots}");
Console.WriteLine($"MotsFrequents = {resultat.AnalyserTexteResult?.MotsFrequents}");
// ----- Types de la réponse -----
// Lecture XML sans tenir compte des préfixes (ns0:, a:, s:…)
static class Soap
{
public static XElement? Element(XElement e, string nom) => e.Elements().FirstOrDefault(x => x.Name.LocalName == nom);
public static IEnumerable<XElement> Elements(XElement e, string nom) => e.Elements().Where(x => x.Name.LocalName == nom);
public static string? Text(XElement e, string nom) => Element(e, nom)?.Value;
public static int? Int(XElement e, string nom) => int.TryParse(Text(e, nom), out var v) ? v : null;
public static long? Long(XElement e, string nom) => long.TryParse(Text(e, nom), out var v) ? v : null;
public static double? Double(XElement e, string nom) => double.TryParse(Text(e, nom), NumberStyles.Float, CultureInfo.InvariantCulture, out var v) ? v : null;
public static decimal? Decimal(XElement e, string nom) => decimal.TryParse(Text(e, nom), NumberStyles.Float, CultureInfo.InvariantCulture, out var v) ? v : null;
public static bool? Bool(XElement e, string nom) => Text(e, nom) is string s ? s == "true" || s == "1" : null;
public static int Int(XElement x) => int.Parse(x.Value);
public static long Long(XElement x) => long.Parse(x.Value);
public static double Double(XElement x) => double.Parse(x.Value, CultureInfo.InvariantCulture);
public static decimal Decimal(XElement x) => decimal.Parse(x.Value, CultureInfo.InvariantCulture);
public static bool Bool(XElement x) => x.Value == "true" || x.Value == "1";
}
public class MotFrequent
{
public string? Mot { get; set; }
public int? Nombre { get; set; }
public static MotFrequent FromXml(XElement e) => new MotFrequent
{
Mot = Soap.Text(e, "Mot"),
Nombre = Soap.Int(e, "Nombre"),
};
}
public class MotsFrequents
{
public List<MotFrequent> MotFrequent { get; set; } = new();
public static MotsFrequents FromXml(XElement e) => new MotsFrequents
{
MotFrequent = Soap.Elements(e, "MotFrequent").Select(MotFrequent.FromXml).ToList(),
};
}
public class AnalyserTexteResult
{
public int? Caracteres { get; set; }
public int? Lignes { get; set; }
public int? Mots { get; set; }
public MotsFrequents? MotsFrequents { get; set; }
public static AnalyserTexteResult FromXml(XElement e) => new AnalyserTexteResult
{
Caracteres = Soap.Int(e, "Caracteres"),
Lignes = Soap.Int(e, "Lignes"),
Mots = Soap.Int(e, "Mots"),
MotsFrequents = Soap.Element(e, "MotsFrequents") is XElement x_MotsFrequents ? MotsFrequents.FromXml(x_MotsFrequents) : null,
};
}
public class AnalyserTexteReponse
{
public AnalyserTexteResult? AnalyserTexteResult { get; set; }
public static AnalyserTexteReponse FromXml(XElement e) => new AnalyserTexteReponse
{
AnalyserTexteResult = Soap.Element(e, "AnalyserTexteResult") is XElement x_AnalyserTexteResult ? AnalyserTexteResult.FromXml(x_AnalyserTexteResult) : null,
};
}
POSTEnChiffresRomainsÉcrit un nombre de 1 à 3999 en chiffres romains.
SOAPAction : http://koakdesign.info/ws/textes/ITextes/EnChiffresRomains
Entrée (EnChiffresRomains)
| Champ | Type | |
|---|---|---|
nombre | int | facultatif |
Sortie (EnChiffresRomainsResponse)
| Champ | Type | |
|---|---|---|
EnChiffresRomainsResult | string | facultatif |
Exemples XML
Requête
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns0="http://koakdesign.info/ws/textes">
<soap:Body>
<ns0:EnChiffresRomains>
<!-- facultatif -->
<ns0:nombre>0</ns0:nombre>
</ns0:EnChiffresRomains>
</soap:Body>
</soap:Envelope>Réponse (forme renvoyée par le serveur)
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns0="http://koakdesign.info/ws/textes">
<soap:Body>
<ns0:EnChiffresRomainsResponse>
<!-- facultatif -->
<ns0:EnChiffresRomainsResult>?</ns0:EnChiffresRomainsResult>
</ns0:EnChiffresRomainsResponse>
</soap:Body>
</soap:Envelope>Remplissez les champs puis envoyez : la requête part du site vers https://koakdesign.info/ws_demo/textes.php.
Un champ facultatif laissé vide n’est pas envoyé.
XML envoyé (modifiable à la main)
; =========================================================
; Appel de l'opération SOAP EnChiffresRomains et lecture de la réponse
; PureBasic 6.x (avant 6.00 : InitNetwork() au démarrage)
; =========================================================
EnableExplicit
; ----- Types de la réponse -----
Structure EnChiffresRomainsReponse
EnChiffresRomainsResult.s
EndStructure
; ----- Lecture XML : outils communs -----
Procedure.s KD_Nom(*Noeud) ; nom sans préfixe : « a:Nom » -> « Nom »
Protected Nom.s = GetXMLNodeName(*Noeud)
ProcedureReturn StringField(Nom, CountString(Nom, ":") + 1, ":")
EndProcedure
Procedure KD_Enfant(*Parent, Nom.s) ; premier sous-élément de ce nom (0 si absent)
Protected *N = ChildXMLNode(*Parent)
While *N
If XMLNodeType(*N) = #PB_XML_Normal And KD_Nom(*N) = Nom
ProcedureReturn *N
EndIf
*N = NextXMLNode(*N)
Wend
ProcedureReturn 0
EndProcedure
Procedure KD_PremierElement(*Parent) ; premier sous-élément, quel que soit son nom
Protected *N = ChildXMLNode(*Parent)
While *N And XMLNodeType(*N) <> #PB_XML_Normal
*N = NextXMLNode(*N)
Wend
ProcedureReturn *N
EndProcedure
Procedure.s KD_Texte(*Parent, Nom.s) ; texte d'un sous-élément ("" si absent)
Protected *N = KD_Enfant(*Parent, Nom)
If *N
ProcedureReturn GetXMLNodeText(*N)
EndIf
ProcedureReturn ""
EndProcedure
; ----- Lecture de la réponse dans les structures -----
Procedure KD_Lire_EnChiffresRomainsReponse(*Noeud, *R.EnChiffresRomainsReponse)
Protected *E
*R\EnChiffresRomainsResult = KD_Texte(*Noeud, "EnChiffresRomainsResult")
EndProcedure
; ----- Appel -----
Procedure.i KD_EnChiffresRomains(Enveloppe.s, *R.EnChiffresRomainsReponse)
Protected Requete, Xml, *Corps, *Rep, Reponse.s, Ok = #False
NewMap Entetes.s()
Entetes("Content-Type") = "text/xml; charset=utf-8"
Entetes("SOAPAction") = ~"\"http://koakdesign.info/ws/textes/ITextes/EnChiffresRomains\""
Requete = HTTPRequest(#PB_HTTP_Post, "https://koakdesign.info/ws_demo/textes.php", Enveloppe, 0, Entetes())
If Requete
Reponse = HTTPInfo(Requete, #PB_HTTP_Response)
FinishHTTP(Requete)
Xml = ParseXML(#PB_Any, Reponse)
If Xml And XMLStatus(Xml) = #PB_XML_Success
*Corps = KD_Enfant(MainXMLNode(Xml), "Body")
*Rep = 0 : If *Corps : *Rep = KD_PremierElement(*Corps) : EndIf
If *Rep And KD_Nom(*Rep) = "Fault"
Debug "Erreur SOAP : " + KD_Texte(*Rep, "faultstring")
ElseIf *Rep
KD_Lire_EnChiffresRomainsReponse(*Rep, *R)
Ok = #True
EndIf
FreeXML(Xml)
Else
Debug "Réponse illisible : " + Reponse
EndIf
EndIf
ProcedureReturn Ok
EndProcedure
; ----- Exemple -----
Define Enveloppe.s = ~"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns0=\"http://koakdesign.info/ws/textes\">\n <soap:Body>\n <ns0:EnChiffresRomains>\n <!-- facultatif -->\n <ns0:nombre>0</ns0:nombre>\n </ns0:EnChiffresRomains>\n </soap:Body>\n</soap:Envelope>"
Define Resultat.EnChiffresRomainsReponse
If KD_EnChiffresRomains(Enveloppe, @Resultat)
Debug "EnChiffresRomainsResult = " + Resultat\EnChiffresRomainsResult
EndIf
// =========================================================
// Appel de l'opération SOAP EnChiffresRomains et lecture de la réponse
// .NET 6 et plus (programme console, instructions de premier niveau)
// Variante : « dotnet-svcutil <adresse du WSDL> » génère un client complet.
// =========================================================
using System.Globalization;
using System.Net.Http.Headers;
using System.Text;
using System.Xml.Linq;
var enveloppe = @"<?xml version=""1.0"" encoding=""utf-8""?>
<soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:ns0=""http://koakdesign.info/ws/textes"">
<soap:Body>
<ns0:EnChiffresRomains>
<!-- facultatif -->
<ns0:nombre>0</ns0:nombre>
</ns0:EnChiffresRomains>
</soap:Body>
</soap:Envelope>";
using var http = new HttpClient();
var requete = new HttpRequestMessage(HttpMethod.Post, "https://koakdesign.info/ws_demo/textes.php");
requete.Content = new StringContent(enveloppe, Encoding.UTF8, "text/xml");
requete.Headers.Add("SOAPAction", "\"http://koakdesign.info/ws/textes/ITextes/EnChiffresRomains\"");
using var reponse = await http.SendAsync(requete);
var doc = XDocument.Parse(await reponse.Content.ReadAsStringAsync());
var corps = doc.Root!.Elements().First(x => x.Name.LocalName == "Body").Elements().First();
if (corps.Name.LocalName == "Fault")
{
Console.WriteLine("Erreur SOAP : " + Soap.Text(corps, "faultstring"));
return;
}
var resultat = EnChiffresRomainsReponse.FromXml(corps);
Console.WriteLine($"EnChiffresRomainsResult = {resultat.EnChiffresRomainsResult}");
// ----- Types de la réponse -----
// Lecture XML sans tenir compte des préfixes (ns0:, a:, s:…)
static class Soap
{
public static XElement? Element(XElement e, string nom) => e.Elements().FirstOrDefault(x => x.Name.LocalName == nom);
public static IEnumerable<XElement> Elements(XElement e, string nom) => e.Elements().Where(x => x.Name.LocalName == nom);
public static string? Text(XElement e, string nom) => Element(e, nom)?.Value;
public static int? Int(XElement e, string nom) => int.TryParse(Text(e, nom), out var v) ? v : null;
public static long? Long(XElement e, string nom) => long.TryParse(Text(e, nom), out var v) ? v : null;
public static double? Double(XElement e, string nom) => double.TryParse(Text(e, nom), NumberStyles.Float, CultureInfo.InvariantCulture, out var v) ? v : null;
public static decimal? Decimal(XElement e, string nom) => decimal.TryParse(Text(e, nom), NumberStyles.Float, CultureInfo.InvariantCulture, out var v) ? v : null;
public static bool? Bool(XElement e, string nom) => Text(e, nom) is string s ? s == "true" || s == "1" : null;
public static int Int(XElement x) => int.Parse(x.Value);
public static long Long(XElement x) => long.Parse(x.Value);
public static double Double(XElement x) => double.Parse(x.Value, CultureInfo.InvariantCulture);
public static decimal Decimal(XElement x) => decimal.Parse(x.Value, CultureInfo.InvariantCulture);
public static bool Bool(XElement x) => x.Value == "true" || x.Value == "1";
}
public class EnChiffresRomainsReponse
{
public string? EnChiffresRomainsResult { get; set; }
public static EnChiffresRomainsReponse FromXml(XElement e) => new EnChiffresRomainsReponse
{
EnChiffresRomainsResult = Soap.Text(e, "EnChiffresRomainsResult"),
};
}
POSTConvertirCasseMet un texte en majuscules, minuscules ou en « Titre ».
SOAPAction : http://koakdesign.info/ws/textes/ITextes/ConvertirCasse
Entrée (ConvertirCasse)
| Champ | Type | |
|---|---|---|
texte | string | facultatif |
mode | enum: Majuscules|Minuscules|Titre | facultatif |
Sortie (ConvertirCasseResponse)
| Champ | Type | |
|---|---|---|
ConvertirCasseResult | string | facultatif |
Exemples XML
Requête
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns0="http://koakdesign.info/ws/textes">
<soap:Body>
<ns0:ConvertirCasse>
<!-- facultatif -->
<ns0:texte>?</ns0:texte>
<!-- facultatif -->
<ns0:mode>Majuscules</ns0:mode>
</ns0:ConvertirCasse>
</soap:Body>
</soap:Envelope>Réponse (forme renvoyée par le serveur)
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns0="http://koakdesign.info/ws/textes">
<soap:Body>
<ns0:ConvertirCasseResponse>
<!-- facultatif -->
<ns0:ConvertirCasseResult>?</ns0:ConvertirCasseResult>
</ns0:ConvertirCasseResponse>
</soap:Body>
</soap:Envelope>Remplissez les champs puis envoyez : la requête part du site vers https://koakdesign.info/ws_demo/textes.php.
Un champ facultatif laissé vide n’est pas envoyé.
XML envoyé (modifiable à la main)
; =========================================================
; Appel de l'opération SOAP ConvertirCasse et lecture de la réponse
; PureBasic 6.x (avant 6.00 : InitNetwork() au démarrage)
; =========================================================
EnableExplicit
; ----- Types de la réponse -----
Structure ConvertirCasseReponse
ConvertirCasseResult.s
EndStructure
; ----- Lecture XML : outils communs -----
Procedure.s KD_Nom(*Noeud) ; nom sans préfixe : « a:Nom » -> « Nom »
Protected Nom.s = GetXMLNodeName(*Noeud)
ProcedureReturn StringField(Nom, CountString(Nom, ":") + 1, ":")
EndProcedure
Procedure KD_Enfant(*Parent, Nom.s) ; premier sous-élément de ce nom (0 si absent)
Protected *N = ChildXMLNode(*Parent)
While *N
If XMLNodeType(*N) = #PB_XML_Normal And KD_Nom(*N) = Nom
ProcedureReturn *N
EndIf
*N = NextXMLNode(*N)
Wend
ProcedureReturn 0
EndProcedure
Procedure KD_PremierElement(*Parent) ; premier sous-élément, quel que soit son nom
Protected *N = ChildXMLNode(*Parent)
While *N And XMLNodeType(*N) <> #PB_XML_Normal
*N = NextXMLNode(*N)
Wend
ProcedureReturn *N
EndProcedure
Procedure.s KD_Texte(*Parent, Nom.s) ; texte d'un sous-élément ("" si absent)
Protected *N = KD_Enfant(*Parent, Nom)
If *N
ProcedureReturn GetXMLNodeText(*N)
EndIf
ProcedureReturn ""
EndProcedure
; ----- Lecture de la réponse dans les structures -----
Procedure KD_Lire_ConvertirCasseReponse(*Noeud, *R.ConvertirCasseReponse)
Protected *E
*R\ConvertirCasseResult = KD_Texte(*Noeud, "ConvertirCasseResult")
EndProcedure
; ----- Appel -----
Procedure.i KD_ConvertirCasse(Enveloppe.s, *R.ConvertirCasseReponse)
Protected Requete, Xml, *Corps, *Rep, Reponse.s, Ok = #False
NewMap Entetes.s()
Entetes("Content-Type") = "text/xml; charset=utf-8"
Entetes("SOAPAction") = ~"\"http://koakdesign.info/ws/textes/ITextes/ConvertirCasse\""
Requete = HTTPRequest(#PB_HTTP_Post, "https://koakdesign.info/ws_demo/textes.php", Enveloppe, 0, Entetes())
If Requete
Reponse = HTTPInfo(Requete, #PB_HTTP_Response)
FinishHTTP(Requete)
Xml = ParseXML(#PB_Any, Reponse)
If Xml And XMLStatus(Xml) = #PB_XML_Success
*Corps = KD_Enfant(MainXMLNode(Xml), "Body")
*Rep = 0 : If *Corps : *Rep = KD_PremierElement(*Corps) : EndIf
If *Rep And KD_Nom(*Rep) = "Fault"
Debug "Erreur SOAP : " + KD_Texte(*Rep, "faultstring")
ElseIf *Rep
KD_Lire_ConvertirCasseReponse(*Rep, *R)
Ok = #True
EndIf
FreeXML(Xml)
Else
Debug "Réponse illisible : " + Reponse
EndIf
EndIf
ProcedureReturn Ok
EndProcedure
; ----- Exemple -----
Define Enveloppe.s = ~"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns0=\"http://koakdesign.info/ws/textes\">\n <soap:Body>\n <ns0:ConvertirCasse>\n <!-- facultatif -->\n <ns0:texte>?</ns0:texte>\n <!-- facultatif -->\n <ns0:mode>Majuscules</ns0:mode>\n </ns0:ConvertirCasse>\n </soap:Body>\n</soap:Envelope>"
Define Resultat.ConvertirCasseReponse
If KD_ConvertirCasse(Enveloppe, @Resultat)
Debug "ConvertirCasseResult = " + Resultat\ConvertirCasseResult
EndIf
// =========================================================
// Appel de l'opération SOAP ConvertirCasse et lecture de la réponse
// .NET 6 et plus (programme console, instructions de premier niveau)
// Variante : « dotnet-svcutil <adresse du WSDL> » génère un client complet.
// =========================================================
using System.Globalization;
using System.Net.Http.Headers;
using System.Text;
using System.Xml.Linq;
var enveloppe = @"<?xml version=""1.0"" encoding=""utf-8""?>
<soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:ns0=""http://koakdesign.info/ws/textes"">
<soap:Body>
<ns0:ConvertirCasse>
<!-- facultatif -->
<ns0:texte>?</ns0:texte>
<!-- facultatif -->
<ns0:mode>Majuscules</ns0:mode>
</ns0:ConvertirCasse>
</soap:Body>
</soap:Envelope>";
using var http = new HttpClient();
var requete = new HttpRequestMessage(HttpMethod.Post, "https://koakdesign.info/ws_demo/textes.php");
requete.Content = new StringContent(enveloppe, Encoding.UTF8, "text/xml");
requete.Headers.Add("SOAPAction", "\"http://koakdesign.info/ws/textes/ITextes/ConvertirCasse\"");
using var reponse = await http.SendAsync(requete);
var doc = XDocument.Parse(await reponse.Content.ReadAsStringAsync());
var corps = doc.Root!.Elements().First(x => x.Name.LocalName == "Body").Elements().First();
if (corps.Name.LocalName == "Fault")
{
Console.WriteLine("Erreur SOAP : " + Soap.Text(corps, "faultstring"));
return;
}
var resultat = ConvertirCasseReponse.FromXml(corps);
Console.WriteLine($"ConvertirCasseResult = {resultat.ConvertirCasseResult}");
// ----- Types de la réponse -----
// Lecture XML sans tenir compte des préfixes (ns0:, a:, s:…)
static class Soap
{
public static XElement? Element(XElement e, string nom) => e.Elements().FirstOrDefault(x => x.Name.LocalName == nom);
public static IEnumerable<XElement> Elements(XElement e, string nom) => e.Elements().Where(x => x.Name.LocalName == nom);
public static string? Text(XElement e, string nom) => Element(e, nom)?.Value;
public static int? Int(XElement e, string nom) => int.TryParse(Text(e, nom), out var v) ? v : null;
public static long? Long(XElement e, string nom) => long.TryParse(Text(e, nom), out var v) ? v : null;
public static double? Double(XElement e, string nom) => double.TryParse(Text(e, nom), NumberStyles.Float, CultureInfo.InvariantCulture, out var v) ? v : null;
public static decimal? Decimal(XElement e, string nom) => decimal.TryParse(Text(e, nom), NumberStyles.Float, CultureInfo.InvariantCulture, out var v) ? v : null;
public static bool? Bool(XElement e, string nom) => Text(e, nom) is string s ? s == "true" || s == "1" : null;
public static int Int(XElement x) => int.Parse(x.Value);
public static long Long(XElement x) => long.Parse(x.Value);
public static double Double(XElement x) => double.Parse(x.Value, CultureInfo.InvariantCulture);
public static decimal Decimal(XElement x) => decimal.Parse(x.Value, CultureInfo.InvariantCulture);
public static bool Bool(XElement x) => x.Value == "true" || x.Value == "1";
}
public class ConvertirCasseReponse
{
public string? ConvertirCasseResult { get; set; }
public static ConvertirCasseReponse FromXml(XElement e) => new ConvertirCasseReponse
{
ConvertirCasseResult = Soap.Text(e, "ConvertirCasseResult"),
};
}
