Exemplo n.º 1
0
        public void ParseMessageWithBinaryData()
        {
            string boundary = "-----Test";

            byte[] requestData;
            using (MemoryStream stream = new MemoryStream())
            {
                using (MultipartStreamWriter writer = new MultipartStreamWriter(
                           stream,
                           boundary,
                           Encoding.UTF8))
                {
                    writer.WritePart("text/plain", "my text", Encoding.UTF8);
                    writer.WritePart("application/xml", "<tag/>", Encoding.UTF8);

                    byte[] pdfData = File.ReadAllBytes(@"..\..\..\samples\pdf-test.pdf");

                    List <HeaderField> headers = new List <HeaderField>();
                    headers.Add(new HeaderField("Content-type", "application/pdf"));
                    headers.Add(new HeaderField("Content-id", "PDF"));
                    writer.WritePart(headers, pdfData);

                    writer.WriteCloseDelimiter();
                }

                requestData = stream.ToArray();
            }

            log.DebugFormat("Message body: {0}", Encoding.UTF8.GetString(requestData));

            string requestContentType = string.Format(
                CultureInfo.InvariantCulture,
                "multipart/mixed;boundary=\"{0}\"",
                boundary);

            MultipartMessage message = StartReading(requestContentType, requestData);

            Assert.IsTrue(message.IsMultipart);
            Assert.AreEqual(MultipartMessage.MediaTypeMultipartMixed, message.MediaType);

            MultipartMessagePart part;

            part = reader.ReadNextPart();
            part = reader.ReadNextPart();
            part = reader.ReadNextPart();

            Assert.AreEqual("application/pdf", part.GetHeader("content-type").FieldValue);
            File.WriteAllBytes("test.pdf", part.Data);

            Assert.IsNull(reader.ReadNextPart());
        }
Exemplo n.º 2
0
        public void WriteManuallyPartByPart()
        {
            string boundary = "-----Test";

            using (MemoryStream stream = new MemoryStream())
            {
                using (MultipartStreamWriter writer = new MultipartStreamWriter(
                           stream,
                           boundary,
                           Encoding.UTF8))
                {
                    writer.WritePart("text/plain", "my text", Encoding.UTF8);
                    writer.WritePart("application/xml", "<tag/>", Encoding.UTF8);
                    writer.WriteCloseDelimiter();
                }

                byte[] bytes  = stream.ToArray();
                string result = Encoding.UTF8.GetString(bytes);
                Assert.AreEqual("\r\n-------Test\r\nContent-Type: text/plain\r\n\r\nmy text\r\n-------Test\r\nContent-Type: application/xml\r\n\r\n<tag/>\r\n-------Test--", result);
            }
        }