繁体   English   中英

从C#调用soap服务

[英]calling a soap service from C#

我有工作的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

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

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

看看WCF:Svcutil生成无效的客户端代理,Apache AXIS Web Service,重载操作以供进一步讨论。

简而言之,在Add Service Reference的Advanced页面上使用Add Web Reference:

添加Web引用

对我来说,这个错误看起来像是生成代理文件的问题。 您可能希望使用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方法并获得某种响应。

暂无
暂无

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

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