1

我试图在 c# 中构建一个客户端,该客户端使用 NuSOAP 库与一些远程(p​​hp)服务器和 SOAP 进行通信。这里我使用一个包含某些用户的用户信息的结构/对象:

公共结构用户配置文件{
            公共字符串用户名;
            公共字符串密码;
            公共字符串电子邮件;
            公共字符串站点;
            公共字符串签名;
            公开年龄;
            公共 int 点;

这是PHP代码:

服务器->wsdl->addComplexType(
                '用户资料',
                '复杂类型',
                '结构',
                '全部',
                '',
                大批(
                    'username' => array('name' => 'username', 'type' => 'xsd:string'),
                    'password' => array('name' => 'password', 'type' => 'xsd:string'),
                    'email' => array('name' => 'email', 'type' => 'xsd:string'),
                    'site' => array('name' => 'site', 'type' => 'xsd:string'),
                    'signature' => array('name' => 'signature', 'type' => 'xsd:string'),
                    'age' => array('name' => 'age', 'type' => 'xsd:int'),
                    'points' => array('name' => 'username', 'type' => 'xsd:int'),
                )
);

$server->wsdl->addComplexType(
                '用户配置文件数组',
                '复杂类型',
                '大批',
                '',
                'SOAP-ENC: 数组',
                大批(),
                数组(数组('ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:UserProfile[]')),
                'tns:用户配置文件'
);

$server->register("getUserProfile",
    大批(),
    数组('return' => 'tns:UserProfileArray'),
    $命名空间,
    错误的,
    'rpc',
    错误的,
    '获取用户配置文件对象'
);

函数 getUserProfile(){
    $profile['用户名'] = "用户";
    $profile['password'] = "pass";
    $profile['email'] = "usern@ame";
    $profile['site'] = "u.com";
    $profile['signature'] = "usucsdckme";
    $profile['年龄'] = 111;
    $profile['points'] = time() / 2444;

    返回 $profile;
}

现在我已经有了一个可用的登录功能,我想获取有关登录用户的信息,但我不知道如何获取这些信息。这就是我用来获取用户信息的方法:

            字符串用户 = txtUser.Text;
            字符串 pass = txtPass.Text;
            SimpleService.SimpleService 服务 = 新的 SimpleService.SimpleService();
            如果(服务登录(用户,通过)){
                 //登录

            }

            SoapApp.SimpleService.UserProfile[] user = service.getUserProfile(); // 这行给了我一个例外

            MessageBox.Show(user[0].username + "--" + user[0].points);

getUserProfile() 函数产生错误:

System.Web.Services.Protocols.SoapException 未处理
  Message="无法序列化结果"
  源="系统.Web.服务"

或者我得到类似“无法解析 xml”的错误。

我用于此的文章来自:http://www.sanity-free.org/125/php_webservices_and_csharp_dotnet_soap_clients.html 他们所做的和我尝试做的不同之处在于我只想返回一个对象而不是多个“MySoapObjects”。

我希望有人熟悉这个并可以帮助我,在此先感谢!问候, opx

4

2 回答 2

0

乍一看,问题似乎在于您在 PHP 中设置了 WSDL,以便您的 getUserProfile() 方法返回一个 UserProfile 对象数组。通过查看该方法,它实际上只返回一个 UserProfile 对象……而 C# 期望它是一个对象数组。

简而言之,您的 WSDL 和代码不同步。我认为您需要更改向 WSDL 注册方法的代码:

$server->register("getUserProfile",
    array(),
    array('return' => 'tns:UserProfile'),
    $namespace,
    false,
    'rpc',
    false,
    'Get the user profile object'
);

您还必须将调用 Web 服务的 C# 代码更改为如下内容:

UserProfile user = service.getUserProfile();

然后您可以摆脱 UserProfileArray 类型的整个 WSDL 注册。

于 2009-07-16T14:03:50.343 回答
0

我遇到了同样的问题,这是因为我试图在本地创建一个与从 WebService 返回的对象同名的对象。

相反,我需要做的是允许服务引用管理这个过程,并根据它从 WSDL 中读取的内容为我定义对象。

因此,对于名为“localWS”的服务引用,以下内容适用:

localWS.ServReference ws = new localWS.ServReference();
localWS.ServReference.MyObject obj = localWS.ServReference.MyObject();
obj = localWS.ServReference.CallMethodHere();
于 2010-01-11T13:52:04.240 回答