C# 包含多个部分的HttpWebResponse

C# 包含多个部分的HttpWebResponse,c#,.net,httpwebresponse,C#,.net,Httpwebresponse,我们有一个服务器,可以使用web服务查询照片。维护服务器的人是Java开发人员,但我需要能够使用dotNET查询服务器,因为我正在使用SDK与我们正在运行的现有应用程序集成,她不愿意/无法帮助dotNET 这是我对照片的要求(请注意所需的证书):- 我可以查询服务器没有戏剧,但是HttpWebResponse在一个流中包含两个部分——一个包含照片信息的XML部分和一个JPEG二进制部分。例如:- --WMBMIME2Boundaryurn_uuid_C123 Content-Type: text

我们有一个服务器,可以使用web服务查询照片。维护服务器的人是Java开发人员,但我需要能够使用dotNET查询服务器,因为我正在使用SDK与我们正在运行的现有应用程序集成,她不愿意/无法帮助dotNET

这是我对照片的要求(请注意所需的证书):-

我可以查询服务器没有戏剧,但是HttpWebResponse在一个流中包含两个部分——一个包含照片信息的XML部分和一个JPEG二进制部分。例如:-

--WMBMIME2Boundaryurn_uuid_C123
Content-Type: text/xml; charset=utf-8
Content-Transfer-Encoding: binary
Content-ID: <0.urn:uuid:C123@ibm.com>

<NS1:Envelope xmlns:NS1="http://schemas.xmlsoap.org/soap/envelope/">...</NS1:Envelope>
--WMBMIME2Boundaryurn_uuid_C123
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
Content-ID: <987>

yoya..JFIF...
--WMBMIME2Boundaryurn\u uuid\u C123
内容类型:text/xml;字符集=utf-8
内容传输编码:二进制
内容ID:
...
--WMBMIME2Boundaryurn_uuid_C123
内容类型:应用程序/八位字节流
内容传输编码:二进制
内容ID:
yoya..JFIF。。。
我在这里看到了一些与此相关的问题,但我似乎无法在这些库中使用证书。是否有一种简单的方法来包装输出流,以获得作为独立对象的部分?我很高兴它只是字符串和字节[],我可以自己将其转换为XML和图像

我尝试使用StreamReader,读取第二个空行,即二进制数据开始的位置,然后将原始流中的剩余部分读取到一个文件中,但这不起作用:-/

任何帮助都将不胜感激


谢谢。

您使用的客户机、请求和响应类来自
System.Net
命名空间,与
System.Net.Http
命名空间中的一些较新类相比,它对您的抽象要少一些。像任何新的东西一样,文档和示例更少,但在这个特殊的例子中,我认为使用“MicrosoftWebAPI框架”附带的客户机类将使您受益匪浅

具体来说,WebAPI框架中的响应类称为
HttpResponseMessage
,它有一个属性

HttpResponseMessage response = ...
HttpContent content = response.Content;
if(content is MultipartContent)
{
    // loop through parts
}
您可以在此处找到有关此不同命名空间的更多文档:


一个交易破坏者可能在旧版本的.NET上。我相信WebAPI 2需要最新和最好的版本4.5,但您可能会找到一个适用于版本4的版本。我没有使用过以前的版本,但我认为他们将其分解为多个内容部分所做的分析是非常重要的。

对于其他试图完成类似任务的人,我已经为这个概念验证做了一些变通。这不是最有效的代码,但它在短期内完成了工作,而我正在努力改进它,在我必须完成的所有其他工作之间:-)

使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.IO;
使用System.Linq;
Net系统;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用System.Xml;
名称空间警察照片查看器
{
公共部分类CPV:表格
{
公共CPV()
{
初始化组件();
textIP.Text=Dns.GetHostAddresses(Dns.GetHostName())[0].MapToIPv4().ToString();
textID.Text=System.Security.Principal.WindowsIdentity.GetCurrent().Name;
textMachine.Text=Dns.GetHostName();
textApplication.Text=Application.ProductName;
}
私有映像提取映像(字节[]二进制)
{
int start=0;
用于(;开始<二进制.Length;++开始)
if(((char)binary[start])=='ÿ')
打破
byte[]image=新字节[binary.Length-start];
for(int index=start;index=0;read=stream.ReadByte())
input.Enqueue((字节)读取);
返回input.ToArray();
}
私有WebResponse请求()
{
ServicePointManager.ServerCertificateValidationCallback=新系统.Net.Security.RemoteCertificateValidationCallback(验证);
System.Security.Cryptography.X509Certificates.X509Certificate证书=
System.Security.Cryptography.X509Certificates.X509Certificate.CreateFromCertFile(“wmbuat.crt”);
HttpWebRequest请求=(HttpWebRequest)WebRequest.Create(textURL.Text);
request.Headers.Add(“SOAP:Action”);
request.ContentType=“text/xml”;
request.Accept=“text/xml”;
request.Method=“POST”;
request.ClientCertificates.Add(证书);
GetXml().Save(request.GetRequestStream());
return request.GetResponse();
}
私有静态布尔验证(对象发送方,System.Security.Cryptography.X509Certificates.X509Certificates,System.Security.Cryptography.X509Certificates.X509Chain-chain,System.Net.Security.SslPolicyErrors)
{
返回true;
}
}
}
GetImage最初在按下窗体上的按钮时运行。一旦有结果,它将响应流转换为字节数组,然后搜索“ÿ”的第一个实例(在我们的情况下,它只会出现在JPEG的开头),然后将
HttpResponseMessage response = ...
HttpContent content = response.Content;
if(content is MultipartContent)
{
    // loop through parts
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace COPS_Photo_Viewer
{
    public partial class CPV : Form
    {
        public CPV()
        {
            InitializeComponent();

            textIP.Text = Dns.GetHostAddresses(Dns.GetHostName())[0].MapToIPv4().ToString();
            textID.Text = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
            textMachine.Text = Dns.GetHostName();
            textApplication.Text = Application.ProductName;
        }

        private Image ExtractImage(byte[] binary)
        {
            int start = 0;
            for (; start < binary.Length; ++start)
                if (((char)binary[start]) == 'ÿ')
                    break;

            byte[] image = new byte[binary.Length - start];
            for (int index = start; index < binary.Length; ++index)
                image[index - start] = binary[index];

            return Bitmap.FromStream(new MemoryStream(image));
        }

        private void GetImage(Object sender, EventArgs args)
        {
            WebResponse response = Request();
            byte[] input = ReadStream(response.GetResponseStream());
            photograph.Image = ExtractImage(input);
        }

        private XmlDocument GetXml()
        {
            XmlDocument xml = new XmlDocument();
            xml.LoadXml(...);
            return xml;
        }

        private byte[] ReadStream(Stream stream)
        {
            Queue<byte> input = new Queue<byte>();
            byte[] buffer = new byte[1];

            for (int read = stream.ReadByte(); read >= 0; read = stream.ReadByte())
                input.Enqueue((byte)read);

            return input.ToArray<byte>();
        }

        private WebResponse Request()
        {
            ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(Validate);
            System.Security.Cryptography.X509Certificates.X509Certificate certificate =
            System.Security.Cryptography.X509Certificates.X509Certificate.CreateFromCertFile("wmbuat.crt");

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(textURL.Text);
            request.Headers.Add("SOAP:Action");
            request.ContentType = "text/xml";
            request.Accept = "text/xml";
            request.Method = "POST";
            request.ClientCertificates.Add(certificate);

            GetXml().Save(request.GetRequestStream());
            return request.GetResponse();
        }

        private static Boolean Validate(Object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors errors)
        {
            return true;
        }
    }
}