Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从SOAP响应获取附件并保存文件_C#_Web Services_Soap_Attachment - Fatal编程技术网

C# 从SOAP响应获取附件并保存文件

C# 从SOAP响应获取附件并保存文件,c#,web-services,soap,attachment,C#,Web Services,Soap,Attachment,我正在使用webservice,它在带有附件的SOAP中给出响应。 我用邮递员捕捉到的反应是 --MIMEBoundaryurn_uuid_C455EAC131FBC506CE1521805985220 Content-Type: text/xml; charset=UTF-8 Content-Transfer-Encoding: 8bit Content-ID: <0.urn:uuid:C455EAC131FBC506CE1521805985221@apache.org> <

我正在使用webservice,它在带有附件的SOAP中给出响应。 我用邮递员捕捉到的反应是

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

但是文件正在被破坏。

处理完这个问题后,我在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;
        }
使用(HttpWebResponse=(HttpWebResponse)request.GetResponse())
{
字符串retVal=readMultipart(响应).Result;
}
异步静态任务readMultipart(HttpWebResponse httpResponse)
{
var content=newstreamcontent(httpResponse.GetResponseStream());
添加(“内容类型”,httpResponse.ContentType);
MultipartMemoryStreamProvider multipart=新的MultipartMemoryStreamProvider();
Task.Factory.StartNew(()=>multipart=content.ReadAsMultipartAsync().Result,
取消令牌。无,
TaskCreationOptions.LongRunning,//保证独立线程
TaskScheduler.Default)
.Wait();
字符串filename=“”;
字符串json=await multipart.Contents[0]。ReadAsStringAsync();
字符串路径=HttpContext.Current.Server.MapPath(“/test.jpeg”);
byte[]fileData=multipart.Contents[1]。ReadAsByteArrayAsync().Result;
System.IO.File.writealBytes(路径、文件数据);
返回json;
}

我怀疑pdf是ascii编码的。试试UTF-8。太棒了,它能用!!我为此奋斗了很多,谢谢你
 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;
        }