MimeKit. Is it possible to read multipart/related content?

2k views Asked by At

I am trying to read the content of a document in sharepoint and I am reusing a Web Service named cellstorage.csv/CellStorageService

The response is a multipart, containing one XML part and one binary part.

I'd like to read the binary part.

The content type is:

Content-Type: multipart/related; type="application/xop+xml"; boundary="urn:uuid:a192024b-547a-584b-a56c-b05f25c22fdf"; start="<[email protected]>"; start-Info="text/xml; charset=utf-8"

The returned content is:

--uuid:acf0b1de-4e59-4304-ba6e-23a7a4e2a9cc+id=1
Content-ID: <http://tempuri.org/0>
Content-Transfer-Encoding: 8bit
Content-Type: application/xop+xml;charset=utf-8;type="text/xml"

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body>
<ResponseVersion Version="2" MinorVersion="3" 
xmlns="http://schemas.microsoft.com/sharepoint/soap/"/><ResponseCollection 
WebUrl="

...
></SubResponse></Response></ResponseCollection></s:Body></s:Envelope>

--uuid:acf0b1de-4e59-4304-ba6e-23a7a4e2a9cc+id=1
Content-ID: <http://tempuri.org/1/636403683925268534>
Content-Transfer-Encoding: binary
Content-Type: application/octet-stream

As I am already using MimeKit in this project, I have tried to load this into Mimekit, so that it can parse it:

           ContentType contentType1 = ContentType.Parse("application/xop+xml");
            MimeEntity entity1 = MimeEntity.Load(contentType1, new MemoryStream(bytes));

            ContentType contentType = ContentType.Parse("application/octet-stream");
            MimeEntity entity = MimeEntity.Load(contentType, new MemoryStream(bytes));

            MimeParser parser = new MimeParser(new MemoryStream(bytes), MimeFormat.Entity);
            //var message = parser.ParseMessage();
            var entity3 = parser.ParseEntity();

When I look at the result in the debugger, the 3 entities (entiy, entity1 and entity3) all look the same, and it seems that only the xml content have been parsed.

I can't get the binary part.

Could you please let me know if MimeKit can be used to parse this type of content, and if I am parsing this correctly?

Many thanks

Gilles

1

There are 1 answers

0
jstedfast On

Since classes like HttpWebResponse take care of parsing the HTTP headers (which includes the Content-Type header) and only offer a content stream to consume, MimeKit provides a way to deal with this using the following two static methods on MimeEntity:

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

Here's how you might use these methods:

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

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