从 SOAP 响应中获取附件并保存文件 [英] Get Attachment from SOAP Response and save file

查看:53
本文介绍了从 SOAP 响应中获取附件并保存文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Web 服务,该服务在带有附件的 SOAP 中给出响应.我使用 PostMan 捕获的响应是

I am consuming webservice which is giving response in SOAP with Attachment. Response which i captured using PostMan is

--MIMEBoundaryurn_uuid_C455EAC131FBC506CE1521805985220
Content-Type: text/xml; charset=UTF-8
Content-Transfer-Encoding: 8bit
Content-ID: <0.urn:uuid:C455EAC131FBC506CE1521805985221@apache.org>

<?xml version='1.0' encoding='UTF-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><ns:downloadDocInFileResponse xmlns:ns="http://provider.ws.jts.omni.newgen.com"><ns:return><swa:fileName xmlns:swa="http://provider.ws.jts.omni.newgen.com"><swa:graph>urn:uuid:C455EAC131FBC506CE1521805985219</swa:graph><swa:message>file download on server successfully</swa:message></swa:fileName></ns:return></ns:downloadDocInFileResponse></soapenv:Body></soapenv:Envelope>
--MIMEBoundaryurn_uuid_C455EAC131FBC506CE1521805985220
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
Content-ID: <urn:uuid:C455EAC131FBC506CE1521805985219>

II*----binary content----

我的代码是

    using (WebResponse response = request.GetResponse())
                {
                    using (StreamReader rd = new StreamReader(response.GetResponseStream()))
                    {
                        string soapResult = rd.ReadToEnd();
                        retVal = soapResult;
                        byte[] temp = Encoding.ASCII.GetBytes(soapResult);
                    }
                }
 var binaryString = ToBinary(ConvertToByteArray(retVal, Encoding.ASCII));

            byte[] bytes = Convert.FromBase64String(binaryString);
            string path = HttpContext.Current.Server.MapPath("/test.pdf");

            File.WriteAllBytes(path, bytes);

但文件已损坏.

推荐答案

在解决这个问题后,我在 MultipartMemoryStreamProvider 的帮助下找到了解决方案

After working on this i found the solution with the help of MultipartMemoryStreamProvider

 using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {

           String retVal = readMultipart(response).Result;
        }

async static Task<string> readMultipart(HttpWebResponse httpResponse)
        {
            var content = new StreamContent(httpResponse.GetResponseStream());
            content.Headers.Add("Content-Type", httpResponse.ContentType);


            MultipartMemoryStreamProvider multipart = new MultipartMemoryStreamProvider();
            Task.Factory.StartNew(() => multipart = content.ReadAsMultipartAsync().Result,
               CancellationToken.None,
               TaskCreationOptions.LongRunning, // guarantees separate thread
               TaskScheduler.Default)
               .Wait();
            String filename = "";
            String json = await multipart.Contents[0].ReadAsStringAsync();


            string path = HttpContext.Current.Server.MapPath("/test.jpeg");
            byte[] fileData = multipart.Contents[1].ReadAsByteArrayAsync().Result;
            System.IO.File.WriteAllBytes(path, fileData);
            return json;
        }

这篇关于从 SOAP 响应中获取附件并保存文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆