2012-11-21 171 views
0

我正在研究一个asp.net的web应用程序,用C#编译。我必须实现使用PHP创建的第三方Web服务。这是一个非常简单的服务,只包含一个功能。我使用wsdl添加了服务引用,到目前为止情况非常好。基于PHP的web服务调用

当我用正确的参数调用web服务函数时,它总是返回null。我开始使用SoapUI进行故障排除。我从应用程序中捕获肥皂消息并将其粘贴到SoapUI中,执行它并返回正确的消息。使用招我发现了一些从网络服务的响应怪异如图所示的原始输出:

HTTP/1.1 200 OK 
Date: Wed, 21 Nov 2012 15:24:31 GMT 
Server: Apache/2.2.16 (Unix) mod_ssl/2.2.16 OpenSSL/0.9.8o 
X-Powered-By: PHP/5.2.13-pl1-gentoo 
Expires: Thu, 19 Nov 1981 08:52:00 GMT 
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 
Pragma: no-cache 
Set-Cookie: PHPSESSID=ddc342cfe7e56e77456fe31b758bf3de; path=/ 
Vary: Accept-Encoding,User-Agent 
Content-Encoding: gzip 
Content-Length: 812 
Keep-Alive: timeout=15, max=100 
Connection: Keep-Alive 
Content-Type: text/xml; charset=utf-8 

?????????KS?0???`r?~?<??? ?I 
I?????0??+?.????kK.????[E?????[???????}??g4 
?1J???~w?i??<?M?+w??[>]ziIc???.? 
???yvi?"x? F??d?Y?aR,4?X? 
[UQ^F)?$`? 
7??[?F"?$??h???S?a??4??Q?E??6Td,t6%Hg??w/)??????]??G* ?l[??&6?0?$??>??????~?????:??6??W#?a????E?G? 
s??Z????§o?_??c??\???-???)?????cc??w???/??f??}?)??r???????T?/??? m??K??8? ?X?/F8?<???:?m???&f ?Z#[31?*?X,c?Z??0h"??aFb.?<??p??a???Q?B?r>????Z??5??6???????n\y?d?.??\??Hc]?? 
Z,?x??l???g?Q?*&???1?)??????^?????v??pQ???_y~??%??????*? 
>???;??6?+?>???RQq?????a?(?Z????C?5???G??Ce??H?9??xYL|"??i? 
e8?Vk???s???AK^?e~?? 
??(??Lt???r???vs????7??d?w???Jj-B????pt????c??MBi?s)Mo?.??^?aB3?x8&??:_K|???5???)[?M?Xc?j?zX?=G?i/??TO???g????5??c0??w???T?? 

标题正确显示。响应被编码并需要被解码。 SoapUI和Fiddler都能够解码响应,但代理类不能并且返回null。

我该如何克服这个问题?任何帮助是极大的赞赏!

编辑:

方式的服务被称为:

LisenceServiceFR.ServiceRegistration_PortTypeClient client = new LisenceServiceFR.ServiceRegistration_PortTypeClient(); 
LisenceServiceFR.aVehicleInfo info = client.getVehicleInfo("xxx", "xxx", licensePlate, "localhost"); 

编辑2:

从提琴手的响应XML。

<?xml version="1.0" encoding="UTF-8"?> 
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://services.audaconfr.com/ServiceRegistration.wsdl"> 
    <SOAP-ENV:Body> 
     <SOAP-ENV:getVehicleInfoResponse> 
      <aVehicle> 
       <ns1:errorCode>200</ns1:errorCode> 
       <ns1:errorMessage>Success</ns1:errorMessage> 
       <ns1:vehicleXml> 
      &lt;vehicule&gt; 
       &lt;carr&gt;MONOSPACE COMPACT&lt;/carr&gt; 
       &lt;carr_cg&gt;CI&lt;/carr_cg&gt; 
       &lt;co2&gt;152&lt;/co2&gt; 
       <!-- etc --> 
      &lt;/vehicule&gt; 
     </ns1:vehicleXml> 
      </aVehicle> 
     </SOAP-ENV:getVehicleInfoResponse> 
    </SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 
+0

查看头文件'Content-Encoding:gzip' - 有效载荷被压缩。你能告诉你如何致电该服务?我猜代理应该自动解压缩它。 – Jan

+1

_“当我使用正确的参数调用Web服务函数时,它总是返回null。”_ - 我猜测有效负载解压缩正确,但在反序列化过程中响应无效。您可以显示解压缩XML为Fiddler显示它吗? – CodeCaster

+0

偏离主题,但您可能需要将“Lisence”的拼写更正为“许可证” – StingyJack

回答

0

我结束了使用的HttpWebRequest调用web服务:

System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); 
doc.InnerXml = xml; 
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(endPoint); 
req.Timeout = 100000000; 

if (proxy != null) 
    req.Proxy = new WebProxy(proxy, true); 

req.Headers.Add("SOAPAction", ""); 
req.ContentType = "application/soap+xml;charset=\"utf-8\""; 
req.Accept = "application/x-www-form-urlencoded"; 
req.Method = "POST"; 
Stream stm = req.GetRequestStream(); 
doc.Save(stm); 
stm.Close(); 
WebResponse resp = req.GetResponse(); 
stm = resp.GetResponseStream(); 
StreamReader r = new StreamReader(stm); 
string responseData = r.ReadToEnd(); 

XDocument response = XDocument.Parse(responseData); 

/* extract data from response */ 

这不是我一直在寻找的解决方案,但就像是一个魅力的作品。