我必须做什么才能从soap mtom xop回复中获取附件

问题描述 投票:0回答:1

我从 Soap 服务处得到回复(如下)。我第一次使用 mtom xop 肥皂响应类型。我知道如何获取 Content-Type:、Content-Transfer-Encoding:、Content-ID: 但我看不到任何二进制数据(附件),我相信我错过了一些东西。

    --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

我尝试该服务返回一些内容并在 SoapUI 中工作,但仍然不知道如何获取该附件。 应对时应注意什么? 最好的问候

c# soap attachment mtom xop
1个回答
0
投票

解析

MIME
多部分消息并提取附件部分的二进制内容。

  1. 解析 MIME Multipart(MimeKit 包可以提供帮助)
  2. 匹配内容 ID (
    Content-ID: <[email protected]>
    ) 提取二进制数据
  3. 提取二进制数据

简单的例子,

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
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.