从C#调用soap服务

时间:2014-03-31 17:24:08

标签: c# php web-services soap

我有一个PHP代码,它调用SOAP服务并且它可以工作。其内容如下:

<?php
try
{
    $client = new SoapClient(null, array(
        'location' => "http://108-168-196-91.mycitrixdemo.net/zdm/services/EveryWanDevice?wsdl",
        'uri' => "http://zdemo2.zenprise.com",
        'login' => "Admin",
        'password'=> "XXXXX"));

    $properties=$client->getDeviceProperties("XXXXXXXX",null);

    for($i=0;$i<count($properties);$i++) {
        printf ("name: %s, value: %s\n" , $properties[$i]->name, $properties[$i]->value);
    }
}
catch (Exception $e)
{
    print_r($e); exit;
}
?>

我需要从C#访问相同的服务。我尝试将Service Reference添加到http://108-168-196-91.mycitrixdemo.net/zdm/services/EveryWanDevice?wsdl,这在我的app.config中添加了以下部分。

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="EveryWanDeviceSoapBinding" />
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://108-168-196-91.mycitrixdemo.net/zdm/services/EveryWanDevice"
            binding="basicHttpBinding" bindingConfiguration="EveryWanDeviceSoapBinding"
            contract="ServiceReference1.DeviceService" name="EveryWanDevice" />
    </client>
</system.serviceModel>

我现在提供了代理类,但我不知道如何设置它们以便我可以调用此服务。

我在C#中执行以下操作:

DeviceService srv = new DeviceServiceClient();//
srv.authenticateUser(new authenticateUserRequest("Admin", "XXXXXX"));

var devices = srv.getDeviceProperties(new getDevicePropertiesRequest("99000067296308", null));

srv.authenticateUser行抛出以下异常:

RPC Message getDeploymentHistoRequest1 in operation getDeploymentHisto1 has an invalid body name getDeploymentHisto. It must be getDeploymentHisto1

我不知道这个错误是什么意思。有人可以帮忙吗?

2 个答案:

答案 0 :(得分:4)

这是由于使用WCF引用与标准服务引用。

请查看WCF: Svcutil generates invalid client proxy, Apache AXIS Web Service, overload operations进一步讨论。

简而言之,在添加服务参考的高级页面上使用添加Web引用

Add Web Reference

答案 1 :(得分:1)

对我来说,这个错误看起来像是生成代理文件的问题。您可能希望使用svcutil重新代理,以确保正确生成代理文件。在您的情况下,Visual Studio Developer Command工具中的命令将如此...

svcutil.exe /language:cs /out:Proxies.cs /config:output.config [service url]

您也不会在第一时间建立安全会话。将绑定更改为以下内容...

<binding name="EveryWanDeviceSoapBinding" 
         closeTimeout="00:01:00" 
         openTimeout="00:01:00" 
         receiveTimeout="00:10:00" 
         sendTimeout="00:01:00" 
         allowCookies="false" 
         bypassProxyOnLocal="false" 
         hostNameComparisonMode="StrongWildcard" 
         maxBufferSize="6553666" 
         maxBufferPoolSize="524288" 
         maxReceivedMessageSize="6553666" 
         messageEncoding="Text" 
         textEncoding="utf-8" 
         transferMode="Buffered" 
         useDefaultWebProxy="true">
    <security mode="Transport">
        <transport clientCredentialType="Basic" 
                   proxyCredentialType="Basic" 
                   realm="" />
        <message clientCredentialType="UserName" 
                 algorithmSuite="Default" />
    </security>
</binding>

绝大多数只是服务绑定的基本默认值。 security部分应该让您建立与服务的安全连接,如此......

var srv = new DeviceServiceClient();

srv.ClientCreditials.UserName.UserName = "Admin";
srv.ClientCreditials.UserName.Password = "XXXXX";

最后,您可以使用您的参数调用getDeviceProperties方法并获得某种响应。

相关问题