I’m calling a SOAP webservice using HttpRequestMessage in .NET 8.
The content of the request is a xml string containing data about a person.
But the data type expected by the webservice is an base64Binary.
If I use the Visual Studio Connected Services to generate the contract classes, the content is expected as a byte[], wich I can generate by simply converting the xmlstring to base64 and then to byte array and the request is sucessfull.
My problem is to pass the byte[] to HttpRequestMessage inside the string content:
Here is my code:
using System.Text;
namespace SigafHttpRequest;
internal class Program
{
private static readonly HttpClient httpClient = new HttpClient();
static async Task Main(string[] args)
{
string url = "https://sigafhomologa.mg.gov.br/webserver/soap_server.php";
string xmlSoapLogin =
@"<?xml version=""1.0"" encoding=""utf-8""?>
<soapenv:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:sig=""sigaf"">
<soapenv:Header/>
<soapenv:Body>
<sig:login soapenv:encodingStyle=""http://schemas.xmlsoap.org/soap/encoding/"">
<user xsi:type=""xsd:string"">315500</user>
<password xsi:type=""xsd:string"">Wt2023</password>
</sig:login>
</soapenv:Body>
</soapenv:Envelope>";
//extract the token from the response and pass into token_login field of the next request
string tokenAccessResult = await PostSOAPRequestAsync(url, xmlSoapLogin);
string xmlData =
"<?xml version="1.0" encoding="utf-8"?>" +
"<root>"
+ " <identificador>"
+ " <stEsferaEnvio>M</stEsferaEnvio>"
+ " <coMunicipioIbge>310000</coMunicipioIbge>"
+ " <dsEmail>[email protected]</dsEmail>"
+ " <noUsuario>usuarioteste</noUsuario>"
+ " <tpXML>DP</tpXML>"
+ " </identificador>"
+ " <paciente>"
+ " <noNome>MARCOS XXXXXX</noNome>"
+ " <noMae>CHIRLENE XXXXXX</noMae>"
+ " <dsLogradouro>ESPERANCA</dsLogradouro>"
+ " <dsBairro>ESPERANCA</dsBairro>"
+ " <nuCep>35000000</nuCep>"
+ " <sgUfNaturalidade>MG</sgUfNaturalidade>"
+ " <coMunicipioIbgeNaturalidade>310000</coMunicipioIbgeNaturalidade>"
+ " <coMunicipioIbge>310000</coMunicipioIbge>"
+ " <dtNascimento>1990-01-01</dtNascimento>"
+ " <nuCpf>11111111111</nuCpf>"
+ " <tbSexo>M</tbSexo>"
+ " <tpAtendimento>B</tpAtendimento>"
+ " </paciente>"
+ "</root>";
var textAsBytes = Encoding.UTF8.GetBytes(xmlData);
var base64 = Convert.ToBase64String(textAsBytes);
var bytes = Convert.FromBase64String(base64);
var byteString = BitConverter.ToString(bytes);
string xmlSoapPersonRequest =
$@"<soapenv:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:sig=""sigaf"">
<soapenv:Header/>
<soapenv:Body>
<sig:recebeDados soapenv:encodingStyle=""http://schemas.xmlsoap.org/soap/encoding/"">
<token_login xsi:type=""xsd:string"">76727072747074727570777177737373727677777675.MzE1NTAw</token_login>
<source xsi:type=""xsd:base64Binary"">{byteString}</source>
</sig:recebeDados>
</soapenv:Body>
</soapenv:Envelope>";
try
{
string result = await PostSOAPRequestAsync(url, xmlSoapPersonRequest);
Console.WriteLine(result);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadKey();
}
private static async Task<string> PostSOAPRequestAsync(string url, string text)
{
using (HttpContent content = new StringContent(text, Encoding.UTF8, "text/xml"))
using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url))
{
request.Content = content;
using (HttpResponseMessage response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead))
{
//response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
}
}
}
The problem is at the line <source xsi:type=””xsd:base64Binary””>{byteString}.
How can I pass a byte array inside this string concatenation?
I’ve tried to pass the base64, but no success, the webserver return “Error on XML structure”: it doesnt interpret the message as it should (the xml content is correct, I’ve checked).