2012-11-21 209 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 */ 

這不是我一直在尋找的解決方案,但就像是一個魅力的作品。