繁体   English   中英

在没有元数据的情况下与WCF服务进行通信

[英]Communicating with WCF Service without Metadata

我正在尝试与第三方WCF服务进行通信。 我无法获得它的WSDL,也无法使用服务引用将其连接。

我已经使用Fiddler监视和模拟流量,并且确实有效,但是我不确定如何将application / msbin1格式解码为可用格式(我希望在解码结束时应为XML)。 我已经安装了WCF Binary插件。 原始文本为(缩短和审查):

@getAllEntriesResponsehttp://tempuri.org/@getAllEntriesResult   a
DomainServices  i)http://www.w3.org/2001/XMLSchema-instance^TotalCount  ^
RootResults b6http://schemas.datacontract.org/...

同时,WCF Binary看起来像(xml树):

getAllEntriesResponse xmlns="http://tempuri.org/"
-- getAllEntriesResult xmlns:a="DomainServices" xmlns:i="http://www.w3.org/2001/XMLSchema-instance
---- a:TotalCount
...

到目前为止,我为此编写的代码是:

        IWebProxy proxy = new WebProxy("127.0.0.1",8888);

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("<url>.sc/binary/getAllEntries?elementID=0");
        request.Method = "GET";
        request.ContentType = "application/msbin1";
        request.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
        request.Proxy = proxy;
        //request.GetRequestStream().Write(b, 0, b.Length);
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        byte[] b = new byte[response.ContentLength];
        response.GetResponseStream().Read(b, 0, b.Length);

        XmlReader binaryreader = XmlDictionaryReader.CreateBinaryReader(b, 0, b.Length, dictionary,  XmlDictionaryReaderQuotas.Max);
        XmlDocument xdoc = new XmlDocument();
        xdoc.Load(binaryreader);
        return xdoc;

我确实得到了Fiddler得到的同样的东西,所以我的请求按预期工作了(据我所知)。 我认为出问题的是字节(b)的格式与我前面提到的格式相同,因此当将其传递给XML时会出错(无论如何我看起来都不像XML,我不确定是否它可以读取此“二进制xml”)。

我得到的异常是带有堆栈跟踪的xdoc.Load(binaryreader)处的“输入源格式不正确”:

at System.Xml.XmlExceptionHelper.ThrowXmlException(XmlDictionaryReader reader, String res, String arg1, String arg2, String arg3)
at System.Xml.XmlBufferReader.ReadValue(XmlBinaryNodeType nodeType, ValueHandle value)
at System.Xml.XmlBinaryReader.ReadNode()
at System.Xml.XmlBinaryReader.Read()
at System.Xml.XmlLoader.LoadNode(Boolean skipOverWhitespace)
at System.Xml.XmlLoader.LoadDocSequence(XmlDocument parentDoc)
at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace)
at System.Xml.XmlDocument.Load(XmlReader reader)
...

注意:词典中填充了485个条目,这些条目(据我所知)是已知重复出现的字符串的速记列表,用于减小传输大小。 我的列表示例是:

                dictionary.Add("mustUnderstand");
                dictionary.Add("Envelope");
                dictionary.Add("http://www.w3.org/2003/05/soap-envelope");
                dictionary.Add("http://www.w3.org/2005/08/addressing");
                dictionary.Add("Header");
                dictionary.Add("Action");
                dictionary.Add("To");
                dictionary.Add("Body");
                ...
                dictionary.Add("Detail");

我不确定目前缺少什么。 我确定它很小,但是我对这些东西还不够熟悉,无法使其正常工作。

任何帮助表示赞赏,谢谢!

经过一番回顾,我意识到一些XML已被解析,但不是全部。 当我一次阅读一个元素时,我意识到它在特定点处破裂。 然后,我检查了我的字节数组,并确定我的字节数组不完整。 将Stream.Read计数与预期数量进行比较,我发现我没有得到完整的字节数组。

将我的Stream阅读器替换为以下响应:

        MemoryStream ms = new MemoryStream();
        Stream stream = response.GetResponseStream();
        stream.CopyTo(ms);
        byte[] b = ms.ToArray();

给我完整的东西。 现在,它可以正确解析为XMLDocument。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM