首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法在C#中提取MTOM/XOP附件

无法在C#中提取MTOM/XOP附件
EN

Stack Overflow用户
提问于 2017-03-17 21:38:18
回答 3查看 2.4K关注 0票数 0

我仍然无法提取MIME附件。请检查下面的MIME消息。这是我们从服务处收到的。

代码语言:javascript
复制
--MIMEBoundary_199ca6b7114b9acca5deb2047d25d5841d4afb7f68281379
Content-Type: application/xop+xml; charset=utf-8; type="text/xml"
Content-Transfer-Encoding: binary
Content-ID: <0.099ca6b7114b9acca5deb2047d25d5841d4afb7f68281379@apache.org>

<?xml version="1.0" encoding="utf-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Header><StateHeader xmlns="http://www.statemef.com/StateGatewayService"><MessageID>12345201704200009962</MessageID><RelatesTo>12345201704200009962</RelatesTo><Action>GetNewAcks</Action><Timestamp>2017-02-11T01:54:51.676-05:00</Timestamp><TestIndicator>T</TestIndicator></StateHeader></soapenv:Header><soapenv:Body><GetNewAcksResponse xmlns="http://www.statemef.com/StateGatewayService"><MoreAvailable>true</MoreAvailable><AcknowledgementListAttachmentMTOM><xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/include" href="cid:299ca6b7114b9acca5deb2047d25d5841d4afb7f68281379@apache.org"></xop:Include></AcknowledgementListAttachmentMTOM></GetNewAcksResponse></soapenv:Body></soapenv:Envelope>
--MIMEBoundary_199ca6b7114b9acca5deb2047d25d5841d4afb7f68281379
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
Content-ID: <299ca6b7114b9acca5deb2047d25d5841d4afb7f68281379@apache.org>
EN

回答 3

Stack Overflow用户

发布于 2017-03-19 09:23:40

步骤1:获取完整的MIME流,即定义boundary参数为MIMEBoundary_199ca6b7114b9acca5deb2047d25d5841d4afb7f68281379Content-Type头部。没有这个,你就是索尔。

如果您使用的是HttpWebRequest之类的工具,请继续执行步骤2。

第2步:按照MimeKit FAQ中的说明进行操作

如何从HTTP web请求中解析多部分/表单数据?

由于像HttpWebResponse这样的类负责解析header(包括Content-Type header),并且只提供一个内容流供消费,因此MimeKit在MimeEntity上使用以下两个静态方法提供了一种方法来处理此问题

代码语言:javascript
复制
public static MimeEntity Load (ParserOptions options, ContentType contentType, Stream content, CancellationToken cancellationToken = default (CancellationToken));

public static MimeEntity Load (ContentType contentType, Stream content, CancellationToken cancellationToken = default (CancellationToken));

下面是如何使用这些方法:

代码语言:javascript
复制
MimeEntity ParseMultipartFormData (HttpWebResponse response)
{
    var contentType = ContentType.Parse (response.ContentType);

    return MimeEntity.Load (contentType, response.GetResponseStream ());
}

一旦有了MimeEntity,就可以将其转换为Multipart并枚举其中的附件,将内容保存到流中,如下所示:

代码语言:javascript
复制
int i = 1;
foreach (var attachment in multipart.OfType<MimePart> ()) {
    string fileName = string.Format ("attachment.{0}.dat", i++);
    using (var stream = File.Create (fileName))
        attachment.ContentObject.DecodeTo (stream);
}
票数 1
EN

Stack Overflow用户

发布于 2017-07-27 13:35:19

该问题仅显示响应正文。要解析它,您应该预先考虑响应头。

例如,它应该如下所示:

代码语言:javascript
复制
MIME-Version: 1.0 
content-type: multipart/related; type="application/xop+xml";start="<http://tempuri.org/0>";boundary="MIMEBoundary_someuniqueID";start-info="text/xml"
Server: Microsoft-IIS/10.0 
X-Powered-By: ASP.NET 
Content-Length:24371900


--MIMEBoundary_someuniqueID
Content-Type: application/xop+xml; charset=utf-8; type="text/xml" Content-Transfer-Encoding: binary
Content-ID: <http://tempuri.org/0>

<soap:Envelope> 
    <someWrapperElt>
        <xop:Include href="cid:uri_of_content"></xop:Include>
    </someWrapperElt>
</soap:Envelope>
--MIMEBoundary_someuniqueID
Content-Type: application/octet-stream 
Content-Transfer-Encoding: binary 
Content-ID: <uri_of_content>

...start.b1n@ry-content-here-etc.fckZ8990832d...
--MIMEBoundary_someuniqueID--

然后将整个响应转换为MemoryStream对象,并使用XmlDictionaryReader对其进行解析。

代码语言:javascript
复制
XmlDictionaryReader mtomReader = XmlDictionaryReader.CreateMtomReader(ms, Encoding.UTF8, XmlDictionaryReaderQuotas.Max);

也就是说,您现在可以从mtomReader对象中提取所需的值,包括附件。

票数 0
EN

Stack Overflow用户

发布于 2017-10-27 05:02:05

您可以在Github WebResponseDerializer组件中使用解析器项目来解析多部分soap消息

1)将soap body标签之间的xml消息复制到xml2charp站点,取反序列化对象。

2)获取响应流并调用,如下所示。

代码语言:javascript
复制
 Byte[] file = File.ReadAllBytes("..\\..\\Data\\ccc.xxx");
        Stream stream = new MemoryStream(file);
        WebResponseDerializer<SIGetImageResponse> deserilizer = new WebResponseDerializer<SIGetImageResponse>(stream);
        SIGetImageResponse ddd = deserilizer.GetData();
        foreach (var item in ddd.ResponseData.AttachmentDescriptor.Attachment)
        {
            String contentId = "<<" + item.ImageData.Include.Href + ">>";
            contentId = contentId.Replace("%40", "@").Replace("cid:", "");
            item.ImageData.Include.XopData = deserilizer.GetAttachment(contentId);
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42859030

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档