2017-03-17 14 views
0

まだMIME添付ファイルを抽出できません。以下のMIMEメッセージを確認してください。我々はサービスから受け取った。C#でMTOM/XOP添付ファイルを抽出できません

--MIMEBoundary_199ca6b7114b9acca5deb2047d25d5841d4afb7f68281379 
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"?><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:[email protected]"></xop:Include></AcknowledgementListAttachmentMTOM></GetNewAcksResponse></soapenv:Body></soapenv:Envelope> 
--MIMEBoundary_199ca6b7114b9acca5deb2047d25d5841d4afb7f68281379 
Content-Type: application/octet-stream 
Content-Transfer-Encoding: binary 
Content-ID: <[email protected]> 

答えて

0

ステップ1:完全 MIMEストリームを取得し、即ちboundaryパラメータを定義Content-TypeヘッダはMIMEBoundary_199ca6b7114b9acca5deb2047d25d5841d4afb7f68281379することができます。それがなければ、あなたはSOLです。私はHTTPウェブからマルチパート/フォームデータを解析する方法を

:MimeKit FAQの指示に従ってください:あなたはHttpWebRequestのようなものを使用している場合

、2

ステップ2に進み要求?

HttpWebResponse様クラスは(Content-Typeヘッダを含む)HTTPヘッダーを解析するの世話をするだけ消費するコンテンツのストリームを提供するため、MimeKitこれはMimeEntityに以下 2つの静的メソッドを使用して対処する方法を提供します。ここで

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

は、あなたがこれらのメソッドを使用する可能性があります方法は次のとおりです。

MimeEntity ParseMultipartFormData (HttpWebResponse response) 
{ 
    var contentType = ContentType.Parse (response.ContentType); 

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

あなたはMimeEntityを持っていたら、あなたはトンそれをキャストすることができますMultipart Oと、このような流れにコンテンツを保存し、内の添付ファイルの列挙:質問は唯一のレスポンスボディを示し

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); 
} 
+0

最新版のMimeKitで.Parse()メソッドが見つかりませんでしたか? (2017年7月)。したがって、私はあきらめ、.NETフレームワークのみを使用し、私の答えはXmlDictionaryReaderを使用して参照してください。 – joedotnot

+0

申し訳ありませんが、MimeEntity.Load() – jstedfast

0

を。それを解析するには、応答ヘッダーの前に追加する必要があります。例えば

、それは次のようになります。のMemoryStreamオブジェクトに全体の応答を変換し、それを解析するためにXmlDictionaryReaderを使用すると

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> 

[email protected] 
--MIMEBoundary_someuniqueID-- 

つまり、添付ファイルを含むmtomReaderオブジェクトから必要な値を抽出できるようになりました。

0

あなたはxml2charpサイトにマルチパートSOAPメッセージ

1)石鹸bodyタグ間のコピーXMLメッセージを解析し、deserilizedオブジェクトを取ることができGithub WebResponseDerializerコンポーネントでパーサープロジェクトを取ることができます。

2)応答ストリームを取得し、以下のように呼び出します。

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); 
     } 
関連する問題