0

SOAP server requires request with multipart/related content type. Code

using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
public class MyController : Controller
{
    public async Task<IActionResult> SendSoapRequest()
    {
        try
        {
            using var client = new HttpClient();
            using var content = new MultipartFormDataContent();
            // Add the SOAP XML
            var soapXml = @"<?xml version=""1.0"" encoding=""utf-8""?>
                            <soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
                                <!-- Your SOAP content here -->
                            </soap:Envelope>";
            var soapContent = new StringContent(soapXml, Encoding.UTF8, "text/xml");
            content.Add(soapContent, "file", "soap.xml");
            // Add any additional files (if needed)
            // var fileBytes = ... // Read your file content
            // var fileContent = new ByteArrayContent(fileBytes);
            // content.Add(fileContent, "file", "filename.ext");
            // Send the request
            var response = await client.PostAsync("https://example.com/your-soap-endpoint", content);
            if (response.IsSuccessStatusCode)
            {
                // Process the success response
                return Ok("SOAP request sent successfully!");
            }
            else
            {
                // Handle the error response
                return BadRequest("Error sending SOAP request.");
            }
        }
        catch (Exception ex)
        {
            // Handle exceptions
            return StatusCode(500, $"An error occurred: {ex.Message}");
        }
    }
}

creates request with content type multipart/form-data . How to create multipart/realated request?

Using ASP .NET 8 MVC controller.

Update

Created code using sample from https://www.codeproject.com/Articles/1166668/Sending-MIME-Messages-to-a-Web-Service-Endpoint-fr

    using var client = new HttpClient(httpClientHandler);
    const string filecontentid = "cid:filecontentid";
    var soapXml = @$"<?xml version=""1.0"" encoding=""utf-8""?>
<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/""
                  xmlns:xro=""http://x-road.eu/xsd/xroad.xsd""
                  xmlns:iden=""http://x-road.eu/xsd/identifiers"">
  <soapenv:Header>
    <xro:protocolVersion>4.0</xro:protocolVersion>
    <xro:userId>EE00000000000</xro:userId>
    <xro:id>5-57</xro:id>
    <xro:service iden:objectType=""SERVICE"">
  </soapenv:Header>
  <soapenv:Body>
  </soapenv:Body>
</soapenv:Envelope>
";

    const string _boundary = "=-glZ6jtnXmVAn68ZSF5H6Zw==";
    MultipartContent _multiPartcontent = new MultipartContent("Related", _boundary);

    HttpContent _xmlContent = new StringContent(soapXml);
    _xmlContent.Headers.ContentType = new MediaTypeHeaderValue("text/xml");
    _xmlContent.Headers.Add("Content-ID", "soapcontentid");
    _multiPartcontent.Add(_xmlContent);
    HttpContent fileContent = new StringContent("xml xml");
    fileContent.Headers.ContentType = new MediaTypeHeaderValue("text/xml");
    fileContent.Headers.Add("Content-ID", filecontentid);
    _multiPartcontent.Add(fileContent);
    var response = await client.PostAsync(url, _multiPartcontent);
    if (response.IsSuccessStatusCode)
    {
      // Process the success response
      return Ok("SOAP request sent successfully "            );
    }
    else
    {
      // Handle the error response
      return BadRequest("Error sending SOAP request. ");
    }

Will it generate proper request?

9
  • 1
    Probably by using new MultipartContent(....) and implementing the rest yourself? MultipartFormDataContent just has some extra helper methods, most of the implementation in on the base type. Commented May 13, 2024 at 6:13
  • Where to find such sample? Content should be Content-Type: multipart/related; boundary="----=_Part_0_18725445.1367829312171"; type="text/xml"; start="<soapPart>" Commented May 13, 2024 at 7:54
  • Use Go to definition on MultipartFormDataContent, see how tiny the class is over MultipartContent, implement your own by copying the code 1-1 with just one difference: instead of form-data, define base class constructor parameter as related. Too bad the subtype parameter is hidden from constructor signatures in MultipartFormDataContent, if it was not hidden, having an extra class would not even be necessary. Commented May 13, 2024 at 8:06
  • Go to definition does not show code, only definitions. I updated question and added proposed code. Is this code OK ? Commented May 13, 2024 at 14:45
  • 1
    HttpClient is first class member of .NET8. Why you wrote that it is not? SOAP is exactly posting specific XML (embedded into SoapEnvelope element). SoapAction header is not required. I tried Visual Studio code generated by add service reference but server throws error that multiple root elements are not supported. Visual Studio is not capable to make multipart/related request, it can send pure XML content only from generated code. Yes, X-Road requires SOAP with attachments request. Commented May 13, 2024 at 19:52

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.