what I must do to get attachment from soap mtom xop response

99 Views Asked by At

I get response (below) from Soap service. I working first time with mtom xop soap response type. I know how to get Content-Type:, Content-Transfer-Encoding:, Content-ID: but I can't see anywhere binary data (attachment), I belive I miss something.

    --MIMEBoundary_bd4ef2988828f615e5e7b8799adfc3e71a130c0112174bdf
Content-Type: application/xop+xml; charset=UTF-8; type="text/xml"
Content-Transfer-Encoding: binary
Content-ID: <[email protected]>

<?xml version='1.0' encoding='UTF-8'?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Header>
        <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
                       soap:mustUnderstand="1">
            <wsse:BinarySecurityToken xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
                                      EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary"
                                      ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3"
                                      wsu:Id="CertId-i delete /wsse:BinarySecurityToken>
            <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
                          Id="Signature-12587">
                <ds:SignedInfo>
                    <ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
                    <ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
                    <ds:Reference URI="#Id-1097530553">
                        <ds:Transforms>
                            <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
                        </ds:Transforms>
                        <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
                        <ds:DigestValue>oyYEgHiM0gxN6NLKk8BU7iHX2NM=</ds:DigestValue>
                    </ds:Reference>
                </ds:SignedInfo>
                <ds:SignatureValue>
i delete
</ds:SignatureValue>
                <ds:KeyInfo Id="KeyId-AA70E4A98852F297B4170685977235537754">
                    <wsse:SecurityTokenReference xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
                                                 wsu:Id="STRId-AA70E4A98852F297B4170685977235537755">
                        <wsse:Reference URI="#CertId-AA70E4A98852F297B4170685977235537753"
                                        ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3"/>
                    </wsse:SecurityTokenReference>
                </ds:KeyInfo>
            </ds:Signature>
        </wsse:Security>
    </soap:Header>
    <soap:Body xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
               wsu:Id="Id-1097530553">
        <pobierzPlikZrzutuRejestruLekowResponse xmlns="http://abcd.gov.pl/1234/ZrzutRejestruLekowWS/"
                                                xmlns:ns2="http://abcd.gov.pl/123456/v20180509"
                                                xmlns:ns4="http://abcd.gov.pl/123456/mt/v20180509"
                                                xmlns:ns3="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
            <status>SUKCES</status>
            <plikZrzutu>
                <xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/include"
                             href="cid:[email protected]"/>
            </plikZrzutu>
        </pobierzPlikZrzutuRejestruLekowResponse>
    </soap:Body>
</soap:Envelope>
--MIMEBoundary_bd4ef2988828f615e5e7b8799adfc3e71a130c0112174bdf
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
Content-ID: <[email protected]>

PK

I try that service return something and in SoapUI work but still don't know how get that attachemnt. What should I pay attention to in response? Best regards

Update For get whole response I'm using following code.

using (HttpWebResponse response = HttpWebResponse)request.GetResponse())
{
    
    var binaryReader = new BinaryReader(response.GetResponseStream());
    byte[] binaryResult = binaryReader.ReadBytes(1000000000);
    string base64String = Convert.ToBase64String(binaryResult);
    string respon = System.Text.Encoding.UTF8.GetString(binaryResult); //I missing whole attachment part beacause can't decode (it's .zip file)
}   

So right know I don't know how I could get whole response, convert it and get part of soap xml and get part of mtom attachment. When I try convert from base64 I'm getting exception "The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters"

1

There are 1 best solutions below

1
Shrembo On

parse the MIME multipart message and extract the binary content of the attachment part.

  1. Parse the MIME Multipart (MimeKit package can help)
  2. Match Content-ID (Content-ID: <[email protected]>) Extract the Binary Data
  3. Extract the Binary Data

simple example,

using MimeKit;

// Assuming 'response' is the raw string response from the SOAP service

var contentTypeHeader = "Content-Type: multipart/related;";
int startIndex = response.IndexOf(contentTypeHeader);
string mimeContent = response.Substring(startIndex);
mimeContent = mimeContent.Replace(contentTypeHeader, "");

// Parse the MIME multipart content
var stream = new MemoryStream(Encoding.UTF8.GetBytes(mimeContent));
MimeMessage mimeMessage = MimeMessage.Load(stream);

// Find the attachment part
MimePart attachmentPart = mimeMessage.BodyParts
.FirstOrDefault(part => part.ContentId == "[email protected]") as MimePart;

if (attachmentPart != null)
{
    using (var memoryStream = new MemoryStream())
    {
        // Write the binary data to a stream
        attachmentPart.Content.DecodeTo(memoryStream);
        byte[] binaryData = memoryStream.ToArray();
        // Now you have the binary data of the attachment
    }
}