gpt4 book ai didi

c# - 从 C# 访问 phpsoapservice

转载 作者:行者123 更新时间:2023-11-30 00:02:55 25 4
gpt4 key购买 nike

好吧,我想制作一个简单的网络服务来搜索数据库并返回数据,我知道我可以使用 mysql 连接器来做到这一点,但这只是为了学习如何使用 SOAP ,这里是 php SOAP 服务器的代码

require_once ('lib/nusoap.php');

$namespace = "http://localhost/webservice/index.php?wsdl";
$server = new soap_server();
$server->configureWSDL("DBQuery");
$server->wsdl->schemaTargetNamespace = $namespace;
$server->register(
'QueryMsg',
array('name'=>'xsd:string'),
array('return'=>'xsd:string'),
$namespace,
false,
'rpc',
'encoded',
'returns data from database');

function QueryMsg($query)
{
$con=mysqli_connect('localhost','root','','webserivce');


if (mysqli_connect_errno()) {
return "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(!isset($query) or strpos(strtolower($query),'select')<=-1)
{
return "invalid order";
}
else
{

mysqli_real_escape_string($con,$query);
$result = mysqli_query($con,$query);
while($row = mysqli_fetch_array($result)) {
$data[] = $row;}

return json_encode($data);

}
}
// create HTTP listener
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);
exit();
?>

当我尝试从 PHP Soap 客户端调用它时,它可以工作,但是当我尝试在 Visual Studio 中添加此 http://localhost/webservice/index.php 作为服务引用以从 C# 应用程序使用它时,我在这里收到错误是

The HTML document does not contain Web service discovery information.
Metadata contains a reference that cannot be resolved: 'http://localhost/webservice/index.php'.
The content type text/xml; charset=ISO-8859-1 of the response message does not match the content type of the binding (application/soap+xml; charset=utf-8). If using a custom encoder, be sure that the IsContentTypeSupported method is implemented properly. The first 700 bytes of the response were: '<?xml version="1.0" encoding="ISO-8859-1"?><SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body><SOAP-ENV:Fault><faultcode xsi:type="xsd:string">SOAP-ENV:Client</faultcode><faultactor xsi:type="xsd:string"></faultactor><faultstring xsi:type="xsd:string">Operation &apos;&apos; is not defined in the WSDL for this service</faultstring><detail xsi:type="xsd:string"></detail></SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope>'.
The remote server returned an error: (500) Internal Server Error.
If the service is defined in the current solution, try building the solution and adding the service reference again.

已解决:嗯,这很简单,实际上有两种方法,要么使用 WCF,要么将编码更改为 ISO-8859-1或者在创建 SOAP 服务器后添加此行 $server->soap_defencoding = 'UTF-8'; 来更改 Web 服务本身的编码

最佳答案

我会尝试使用 SOAP U.I. 等工具添加服务 WSDL。看看你会得到什么样的错误。它比使用 C# 添加 Web 引用更加不可知,并且可能会披露更多有关为什么在客户端级别无法使用它的详细信息。

我很高兴通过提供更多信息来帮助您解决此问题。您是否在运行客户端的同一台计算机上运行此服务?如果它提示无法将文件 http://localhost/webservice/index.php 与某些内容关联起来,我想知道发现过程是否正在尝试评估无法找到的文件。 IE。源 WSDL 中的导入操作指向客户端无法解析的 URL。

关于c# - 从 C# 访问 phpsoapservice,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24867477/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com