调用基于PHP的webservice

时间:2012-11-21 15:50:34

标签: c# web-services

我正在开发一个asp.net webapplication,用C#构建。我必须实现使用PHP创建的第三方Web服务。这是一个非常简单的服务,只包含一个功能。我使用wsdl添加了服务引用,到目前为止一直很好。

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

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:

来自Fiddler的响应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>

1 个答案:

答案 0 :(得分:0)

我最终使用HttpWebRequest来调用webservice:

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

这不是我想要的解决方案,但它的作用就像一个魅力。