.NET解决方案中的nusoap WebService错误

问题描述 投票:0回答:1

我使用nuSOAP WebService创建了一个Laravel,除了.NET解决方案之外,它在所有环境中都能正常工作......

#PHP /app/Http/route.php

$app->group(['prefix' => 'test', 'namespace' => 'App\Http\Controllers'], function () use ($app) {

    $app->get('/', 'TestController@index');
    $app->get('/{any}', 'TestController@index');
    $app->post('/', 'TestController@index');
    $app->post('/{any}', 'TestController@index');

});

#PHP /app/Http/Controllers/TestController.php

<?php namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Common;
use SoapBox\Formatter\Formatter;

class TestController extends Controller
{
    public function __construct()
    {
        $this->server = new \soap_server;
        $end_point = env('END_POINT') . '/test/wsdl';
        $this->server->configureWSDL('test', 'urn:test', $end_point, 'rpc');

        $this->server->register(
            'testMethod',
            [
                'one' => 'xsd:string',
                'two' => 'xsd:string',
            ],
            [
                'return' => 'xsd:array'
            ],
            'urn:testWSDL',
            'urn:testWSDL#testMethod',
            'rpc',
            'encoded',
            'Test Method'
        );
    }

    function index(Request $request, $data = NULL)
    {
        $_SERVER['QUERY_STRING'] = '';
        if ( $request->segment(2) == 'wsdl' ) $_SERVER['QUERY_STRING'] = 'wsdl';
        return $this->server->service(file_get_contents('php://input'));
    }
}

#PHP /app/Http/helpers.php

<?php use SoapBox\Formatter\Formatter;

function arrayToXml($array, $remove_structure = TRUE) {

    $array = Formatter::make($array, Formatter::ARR);

    $ret = $array->toXml();

    if ( $remove_structure )
    {
        $ret = str_replace('<?xml version="1.0" encoding="utf-8"?>', '', $ret);
        $ret = str_replace('<xml>', '', $ret);
        $ret = str_replace('</xml>', '', $ret);
    }

    return trim($ret);
}

function testMethod($one, $two)
{
    return arrayToXml([
        "one"   =>  $one,
        "two"   =>  $two
    ]);
}

如果我尝试使用SoapGUI,我发送:

<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:testWSDL">
   <soapenv:Header/>
   <soapenv:Body>
      <urn:testMethod soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
         <one xsi:type="xsd:string">first</one>
         <two xsi:type="xsd:string">second</two>
      </urn:testMethod>
   </soapenv:Body>
</soapenv:Envelope>

我收到......

<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>
      <ns1:testMethodResponse xmlns:ns1="urn:testWSDL">
         <return xsi:type="xsd:array">
            <one>first</one>
            <two>second</two>
         </return>
      </ns1:testMethodResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

原始响应是:

HTTP/1.0 200 OK
Date: Tue, 02 Oct 2016 13:26:14 GMT
Server: Apache/2.4.18 (Unix) PHP/5.5.36 LibreSSL/2.2.7
X-Powered-By: PHP/5.5.36
X-SOAP-Server: NuSOAP/0.9.5 (1.56)
Content-Length: 537
Cache-Control: no-cache
Connection: close
Content-Type: text/xml; charset=UTF-8

<?xml version="1.0" encoding="UTF-8"?><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><ns1:testMethodResponse xmlns:ns1="urn:testWSDL"><return xsi:type="xsd:array"><one>first</one><two>second</two></return></ns1:testMethodResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>

我也试过soapclient.com/soaptest.html并且运行得很好......但是在.NET解决方案中......我将它添加为Web Reference:

enter image description here

然后添加下一个代码......然后我得到intellisense ...

first param...

second param...

然后单击Click Me按钮并收到此错误:

System.Xml.dll中出现类型为“System.InvalidOperationException”的第一次机会异常System.InvalidOperationException:Error en el documento XML(1,479)。 ---> System.InvalidCastException:No se puede asignar el objeto de tipo System.Xml.XmlNode [] al objeto de tipo System.String。 zh_cn Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReadertest.Read2_testMethodResponse()zh_cn Microsoft.Xml.Serialization.GeneratedAssembly.ArrayOfObjectSerializer3.Deserialize(XmlSerializationReader reader)zh_cn System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader,String encodingStyle,XmlDeserializationEvents事件) --- Fin del seguimiento de la pila delaexcepcióninterna--- en System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader,String encodingStyle,XmlDeserializationEvents events)en System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle)zh_cn System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage message,WebResponse response,Stream responseStream,Boolean asyncCall)zh_cn System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName,Object [] parameters)zh testingSolution.local31.test.testMethod(String one,String two)zh C:\ Documents and Settings \ admin \ Mis documentos \ testingSolution \ Web References \ local31 \ Reference.vb:línea80en testingSolution.Testeos.Button3_Click(Object sender,EventArgs e)c:\ Documents and Settings \ admin \ Mis documentos \ testingSolution \ index.vb: línea50

如果我将返回类型更改为字符串...在.NET解决方案中,我得到:enter image description here

但是在SoapGUI(以及所有其他应用程序)中:

<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>
      <ns1:testMethodResponse xmlns:ns1="urn:testWSDL">
         <return xsi:type="xsd:string">&lt;one>mundo&lt;/one>&lt;two>aunque&lt;/two></return>
      </ns1:testMethodResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我该怎么做才能解决这个问题?因为我需要返回一个数组。谢谢!

php .net xml web-services nusoap
1个回答
0
投票

最后就是这样:

//  whether $methodreturn is a string of literal XML
$this->server->methodreturnisliteralxml = true;

资料来源:PHP NuSOAP response

© www.soinside.com 2019 - 2024. All rights reserved.