Angelo
Abril 10, 2025, 2:01pm
1
Olá, pessoal!
Tenho uma demanda que envolve o consumo de um WebService no padrão SOAP. Sei que a Latromi permite o consumo de APIs no padrão REST, mas, nos materiais que consultei, não encontrei informações sobre WebServices SOAP.
Gostaria de saber:
É possível consumir um WebService SOAP na plataforma?
Caso afirmativo, como posso implementar esse consumo?
Agradeço desde já pela ajuda!
Olá @Angelo !
Sim, é possível consumir um Web Service no formato SOAP.
Você vai precisar ter um pouco de domínio sobre este protocolo, mas basta gerar o XML no formato correto e enviar usando a classe HttpWebRequest
Segue abaixo um exemplo usando a ação Executar Código C# do Formulário:
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Xml;
using System;
// Url do serviço
string url = "https://....";
// Nome do método
string webMethod = "NomeDoMetodo";
// Namespace do serviço
string nspname = "http://...";
// Nome da ação
string action = "http://.../Path/NomeDoMetodo";
// Valores que serão enviados no XML
var parameters = new Dictionary<string, string> {
{ "campo_1", "Valor 1" },
{ "campo_2", "Valor 2" }
};
var httpRequest = (HttpWebRequest)WebRequest.Create(url);
httpRequest.Method = "POST";
httpRequest.ContentType = "application/soap+xml; charset=utf-8";
//httpRequest.Timeout = 10;
string responseResult = null;
var xmlRequestDocument = new XmlDocument();
xmlRequestDocument.LoadXml(CreateSoapEvelope(url, action, nspname, webMethod, parameters));
using (var stream = httpRequest.GetRequestStream())
{
xmlRequestDocument.Save(stream);
}
using (var response = httpRequest.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
responseResult = reader.ReadToEnd();
reader.Close();
}
response.Close();
// Retorna o resultado para um campo da tela
Fields["txtResult"].Value = responseResult;
}
// Este método retorna o XML no formato SOAO 1.2
string CreateSoapEvelope(
string url,
string action,
string nspname,
string methodName,
Dictionary<string, string> parameters
)
{
var postvalues = "";
foreach (var p in parameters)
{
postvalues += string.Format("<{0}>{1}</{0}>\r\n", p.Key, p.Value);
}
string soapStr =
$@"<?xml version=""1.0"" encoding=""utf-8""?>
<soap:Envelope xmlns:soap=""http://www.w3.org/2003/05/soap-envelope"" xmlns:a=""http://www.w3.org/2005/08/addressing"">
<soap:Header>
<a:Action>{action}</a:Action>
<a:To>{url}</a:To>
</soap:Header>
<soap:Body>
<{methodName} xmlns=""{nspname}"">
{postvalues}
</{methodName}>
</soap:Body>
</soap:Envelope>";
return soapStr;
}