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

错误{“找不到名称空间名称''的元素'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

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

报告相同问题?

悬赏问题

  • ¥15 openwrt双栈NAT
  • ¥15 部分网页页面无法显示!
  • ¥15 怎样解决power bi 中设置管理聚合,详细信息表和详细信息列显示灰色,而不能选择相应的内容呢?
  • ¥15 QTOF MSE数据分析
  • ¥15 平板录音机录音问题解决
  • ¥15 请问维特智能的安卓APP在手机上存储传感器数据后,如何找到它的存储路径?
  • ¥15 (SQL语句|查询结果翻了4倍)
  • ¥15 Odoo17操作下面代码的模块时出现没有'读取'来访问
  • ¥50 .net core 并发调用接口问题
  • ¥15 网上各种方法试过了,pip还是无法使用