简体   繁体   English

如何从WCF SOAP响应中提取二进制附件?

[英]How to extract binary attachment from a WCF SOAP response?

I'm writing a .Net 3.5 solution which is consuming a 3rd party WCF web service. 我正在编写一个使用第三方WCF Web服务的.Net 3.5解决方案。 The proxy client for the SOAP service was generated by VisualStudio as a service reference. SOAP服务的代理客户端由VisualStudio生成,作为服务参考。

A SOAP response from the service includes attachments in the data, as I can see it in fiddler. 服务的SOAP响应包括数据中的附件,正如我在提琴手中看到的那样。 The attachments have a href field which points to a CID reference. 附件具有指向CID参考的href字段。 The proxy client that VS 2012 has created when returning the object which includes the attachments doesn't include any binary data, but it does include the href field with the CID reference in it. VS 2012在返回包含附件的对象时创建的代理客户端不包含任何二进制数据,但确实包含带有CID引用的href字段。

As captured using fiddler, this is the SOAP response attachment data including the cid:xxxx ref: 如使用小提琴手捕获的那样,这是SOAP响应附件数据,包括cid:xxxx参考:

<attachments>
      <cmn:attachment href="cid:52b2d8a50035921e80bf1540" len="309" name="DOC1.rtf" type="application/rtf" xmime:contentType="application/rtf"/>
</attachments>

And in the raw output in fiddler, the attachment data can be seen with the matching cid:xxxx ref: 在提琴手的原始输出中,可以使用匹配的cid:xxxx ref看到附件数据:

------=_Part_22_12445037.1389617382038
Content-Type: application/rtf
Content-Location: DOC1.rtf
Content-ID: <52b2d8a50035921e80bf1540>
Content-Transfer-Encoding: binary

{\rtf1\ansi\ansicpg1252\deff0{\fonttbl{\f0\fnil\fcharset0 Calibri;}}
{\*\generator Msftedit 5.41.21.2510;}\viewkind4\uc1\pard\sa200\sl276\slmult1\lang9\f0\fs22 Doc 1\par
\par
Test information inside an attachment for KM retrieval.\par
\par
Here's something else I've written for use in testing.\par
}

Here is a screenshot of the available fields from the returned object: 这是返回对象的可用字段的屏幕截图:

可用字段

How do I access that attachment data so I can actually download the file? 如何访问该附件数据,以便实际上可以下载文件?

Normally with WCF and SOAP, your file binary is encoded as a base64 string somewhere in your response, however I don't see in what you've posted here the kind of long data string I'd be expecting. 通常,使用WCF和SOAP,您的文件二进制文件在响应中的某个位置被编码为base64字符串,但是在您在此处发布的内容中,我看不到我期望的那种长数据字符串。

If what you're showing here is just the header from the response, you might check the body of the response message,too, since the header has a size limit, but the body the message doesn't - it's therefore the likely place for your binary payload. 如果您在此处显示的只是响应的标头,那么您也可能会检查响应消息的正文,因为标头有大小限制,但是消息的正文却没有,因此,它可能是您的二进制有效载荷。

Wouldn't it be easier to return a byte[] in one of the entities you are returning, and then work from that on your client to reconstruct the file? 在要返回的实体之一中返回byte [],然后在客户端上从该实体工作以重建文件,会不会更容易?

Are you using MTOM? 您在使用MTOM吗? Because WCF does not support SwA (Soap with attachments) out of the box. 因为WCF不支持开箱即用的SwA(带有附件的肥皂)。

Turns out that the issue was due to the way the 3rd party API was returning the response, the MTOM data was not correctly formatted so WCF was ignoring the binary data. 事实证明,问题是由于第三方API返回响应的方式所致,MTOM数据的格式不正确,因此WCF忽略了二进制数据。

The way I got around this was by using a bespoke class which basically uses an HttpRequest to talk directly to the web service and extract/parse the binary data out of the response. 我解决此问题的方法是使用定制类,该类基本上使用HttpRequest直接与Web服务对话,并从响应中提取/解析二进制数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM