C# 结构和 NuSOAP(php)
我试图用 C# 构建一个客户端,使用 NuSOAP 库与一些远程 (php) 服务器进行 SOAP 对话。 这里我使用一个包含某些用户的用户信息的结构/对象:
public struct UserProfile { public string username; public string password; public string email; public string site; public string signature; public int age; public int points;
这是 PHP 代码:
server->wsdl->addComplexType( 'UserProfile', 'complexType', 'struct', 'all', '', array( '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( 'UserProfileArray', 'complexType', 'array', '', 'SOAP-ENC:Array', array(), array(array('ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:UserProfile[]')), 'tns:UserProfile' ); $server->register("getUserProfile", array(), array('return' => 'tns:UserProfileArray'), $namespace, false, 'rpc', false, 'Get the user profile object' ); function getUserProfile(){ $profile['username'] = "user"; $profile['password'] = "pass"; $profile['email'] = "usern@ame"; $profile['site'] = "u.com"; $profile['signature'] = "usucsdckme"; $profile['age'] = 111; $profile['points'] = time() / 2444; return $profile; }
现在我已经有了一个有效的登录功能,我想获取有关登录用户的信息,但我不知道如何做获得这些。 这就是我用来获取用户信息的方法:
string user = txtUser.Text; string pass = txtPass.Text; SimpleService.SimpleService service = new SimpleService.SimpleService(); if(service.login(user, pass)){ //logged in } SoapApp.SimpleService.UserProfile[] user = service.getUserProfile(); // THIS LINE GIVES ME AN EXCEPTION MessageBox.Show(user[0].username + "--" + user[0].points);
getUserProfile() 函数会产生错误:
System.Web.Services.Protocols.SoapException was unhandled Message="unable to serialize result" Source="System.Web.Services"
或者我收到类似“无法解析 xml”的错误。
我为此使用的文章来自: http://www.sanity-free.org/125/php_webservices_and_csharp_dotnet_soap_clients.html 他们正在做的事情和我尝试做的事情的区别在于,我只想返回一个对象而不是多个“MySoapObjects”。
我希望有人熟悉这个并可以帮助我,提前致谢! 问候, 奥克斯
Im trying to build a client in c# that talks with some remote (php)server with SOAP using the NuSOAP library.
Here im using a struct/object that will containt the user info of some user:
public struct UserProfile { public string username; public string password; public string email; public string site; public string signature; public int age; public int points;
And this is the PHP Code:
server->wsdl->addComplexType( 'UserProfile', 'complexType', 'struct', 'all', '', array( '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( 'UserProfileArray', 'complexType', 'array', '', 'SOAP-ENC:Array', array(), array(array('ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:UserProfile[]')), 'tns:UserProfile' ); $server->register("getUserProfile", array(), array('return' => 'tns:UserProfileArray'), $namespace, false, 'rpc', false, 'Get the user profile object' ); function getUserProfile(){ $profile['username'] = "user"; $profile['password'] = "pass"; $profile['email'] = "usern@ame"; $profile['site'] = "u.com"; $profile['signature'] = "usucsdckme"; $profile['age'] = 111; $profile['points'] = time() / 2444; return $profile; }
Now I already have a working login function, and I want to get the info about the logged in user but I dont know howto obtain these. This is what im using to get the userinfo:
string user = txtUser.Text; string pass = txtPass.Text; SimpleService.SimpleService service = new SimpleService.SimpleService(); if(service.login(user, pass)){ //logged in } SoapApp.SimpleService.UserProfile[] user = service.getUserProfile(); // THIS LINE GIVES ME AN EXCEPTION MessageBox.Show(user[0].username + "--" + user[0].points);
The getUserProfile() function produces an error:
System.Web.Services.Protocols.SoapException was unhandled Message="unable to serialize result" Source="System.Web.Services"
or I get something like 'cant parse xml' error.
The article I used for this was from: http://www.sanity-free.org/125/php_webservices_and_csharp_dotnet_soap_clients.html
The difference on what they are doing and what I try to do is that I only want to get one object returned instead of multiple 'MySoapObjects'.
I hope someone is familiar with this and could help me, thanks in advance!
Regards,
opx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
乍一看,问题似乎在于您在 PHP 中设置了 WSDL,以便 getUserProfile() 方法返回 UserProfile 对象的数组。 通过查看该方法,它实际上只返回一个 UserProfile 对象...而 C# 期望它是一个对象数组。
简而言之,您的 WSDL 和代码不同步。 我认为您需要更改使用 WSDL 注册方法的代码:
您还必须将调用 Web 服务的 C# 代码更改为如下所示:
然后您可以摆脱 UserProfileArray 的整个 WSDL 注册类型。
At first glance, it looks like the problem is that you set up your WSDL in PHP so that your getUserProfile() method returns an array of UserProfile objects. By looking at the method, it actually only returns a single UserProfile object...and C# is expecting it to be an array of objects.
In short, your WSDL and code are out of sync. I think you need to change the code where you register the method with the WSDL:
You'll also have to change the C# code calling the web service to something like this:
And then you can get rid of the whole WSDL registration for the UserProfileArray type.
我遇到了同样的问题,这是因为我试图在本地创建一个与从 WebService 返回的对象同名的对象。
相反,我需要做的是允许服务引用管理此过程,并根据从 WSDL 读取的内容为我定义对象。
因此,对于名为“localWS”的服务引用,则适用以下内容:
I had the same problem, and it was because I was trying to create an object locally with the same name as the object being returned from the WebService.
What I needed to do instead, was to allow the Service Reference to manage this process, and define the object for me, based on what it read from the WSDL.
So, with a service reference called "localWS", then the following applies: