dougan1465 2019-05-01 04:44
浏览 161
已采纳

错误{“找不到名称空间名称''的元素'faultstring'。 第6行,第126位。“}尝试从C#连接nusoap Web服务时

I try to create a nusoap web service (I use xampp and .net beans) and connect to it using a C# app (use visual studio). but I encounter this error:

{"Server returned an invalid SOAP Fault. Please see InnerException for more details."}

the inner error is:

{"Element 'faultstring' with namespace name '' was not found. Line 6, position 126."}

As this source suggested I changed properties order for function serialize() in class.soap_fault.php file but it still gives the same error.

I tried restarting the computer so I now it is not a catch problem.

Also the only other change I made in nusoap library was changing var $soap_defencoding = 'ISO-8859-1'; to var $soap_defencoding = 'UTF-8'; in nusoap.php file to solve encoding problem.

in C# I have the following code:

SR1.demoPortTypeClient client = new SR1.demoPortTypeClient();
var result = client.gettext("hello");
MessageBox.Show("*" + result + "*");

and in php:

<?php
require 'lib/nusoap.php';
$server=new nusoap_server();
$server->configureWSDL("demo");
$namespace = "demo";
$server->wsdl->schemaTargetNamespace = $namespace;
$server->register(
        "gettext",//name of function
        array("txt"=>'xsd:string'),//inputs
        array("return"=>'xsd:string'),//outputs
        $namespace,// namespace
        false,// soapaction (use default)
        'rpc',// style: rpc or document
        'encoded',// use: encoded or literal
        'Return same text'// description for method
        );

$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);

where gettext function is:

<?php
function gettext($txt)
{
    return $txt;
}

current serialize() function is:

function serialize(){
        $ns_string = '';
        foreach($this->namespaces as $k => $v){
            $ns_string .= "
  xmlns:$k=\"$v\"";
        }
        $return_msg =
            '<?xml version="1.0" encoding="'.$this->soap_defencoding.'"?>'.
            '<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"'.$ns_string.">
".
                '<SOAP-ENV:Body>'.
                '<SOAP-ENV:Fault>'.
                    $this->serialize_val( $this->faultcode,   'faultcode'   ) .
                            $this->serialize_val( $this->faultstring, 'faultstring' ) .
                            $this->serialize_val( $this->faultactor,  'faultactor'  ) .
                            $this->serialize_val( $this->faultdetail, 'detail'      ) .
                '</SOAP-ENV:Fault>'.
                '</SOAP-ENV:Body>'.
            '</SOAP-ENV:Envelope>';
        return $return_msg;
    }

I expected changing order of properties solves the problem, however I still receive the same error. I searched a lot but couldn't find any other solve for the error.

  • 写回答

1条回答 默认 最新

  • douzi1350 2019-05-06 06:21
    关注

    For reason I do not know, there is two definition for function serialize() with same body in two different file. One in class.soap_fault.php and another in nusoap.php and you need to change the method body in the second one (nusoap.php). I do not know is there any condition you need to change other one or not.


    For other ones who may reach this post I add some problems you may encounter and their solve:

    Exception :

    Encodes were different (windows uses 'utf-8' and this uses 'ISO-8859-1')

    Reason: Encoding type are different in server and client.

    Solve: In nusoap.php file changing var $soap_defencoding = 'ISO-8859-1'; to var $soap_defencoding = 'UTF-8';

    See: this source


    Exception :

    Server returned an invalid SOAP Fault. Please see InnerException for more details

    Inner:

    Element ‘faultstring’ with namespace name ” was not found. Line 6, position 126

    Reason: Difference in order of fault properties

    Solve: in nusoap.php change function serialize() to following:

    function serialize() {
          $ns_string = '';
          foreach( $this->namespaces as $k => $v ) {
            $ns_string .= "
      xmlns:$k=\"$v\"";
          }
          $return_msg =
            '<?xml version="1.0" encoding="' . $this->soap_defencoding . '"?>' .
            '<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"' . $ns_string . ">
    " .
              '<SOAP-ENV:Body>' .
                '<SOAP-ENV:Fault>' .
                  $this->serialize_val( $this->faultcode,   'faultcode'   ) .
                  $this->serialize_val( $this->faultstring, 'faultstring' ) .
                  $this->serialize_val( $this->faultactor,  'faultactor'  ) .
                  $this->serialize_val( $this->faultdetail, 'detail'      ) .
                '</SOAP-ENV:Fault>' .
              '</SOAP-ENV:Body>' .
            '</SOAP-ENV:Envelope>';
          return $return_msg;
    }
    

    See: this source


    Exception:

    {"error in msg parsing: xml was empty, didn't parse!"}

    Reason: The following php function is not valid in current version:

    solve: use following code for php version 5.6.0 to before 7.0.0

    $POST_DATA = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : '';
    $server->service($POST_DATA); 
    

    and for version 7.0.0 and later use:

    @$server->service(file_get_contents("php://input"));
    

    See: This source


    Exception:

    {"method ''('function name here'('function name here') not defined in service('' '')"}

    or simply:

    method '' not defined in service.

    Reason: When using C# to connecting to the nusoap web server. It is disable to find methods defined in other files/classes.

    Solve: define all method in the same file as you write web service

    See: This source

    Note: If you know a way to overcome this problem and let define methods in other files/classes (= make cleaner code) please comment it.


    Exception:

    {"Error in deserializing body of reply message for operation 'method name'."}

    Reason: May have different reasons but in my case occure when try to use arrays as property for returned structure. C# will see array of string as string.

    Solve: I solved it by using basic types.

    Note: if you know better way please comment it

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?