C# 使用Web服务时出错,内容类型为“;应用程序/xop+;xml";与预期类型不匹配;text/xml“;

C# 使用Web服务时出错,内容类型为“;应用程序/xop+;xml";与预期类型不匹配;text/xml“;,c#,.net,webservice-client,wcf-client,C#,.net,Webservice Client,Wcf Client,我在为公司购买的产品使用Web服务时遇到了一个奇怪的问题。该产品名为“战役指挥官”,由一家名为“电子邮件愿景”的公司生产。我们正在尝试使用“数据批量更新SOAP API” 每当我尝试调用webservice上的任何方法时,调用实际上都会成功,但客户端在处理响应时失败,我得到一个异常 下面是错误的详细信息,谢谢你们提供的帮助 使用Web引用时出错(旧式webservice客户端) 当将服务作为Web引用使用时,我会收到一个invalidooperationexception,用于我进行的任何调用,

我在为公司购买的产品使用Web服务时遇到了一个奇怪的问题。该产品名为“战役指挥官”,由一家名为“电子邮件愿景”的公司生产。我们正在尝试使用“数据批量更新SOAP API”

每当我尝试调用webservice上的任何方法时,调用实际上都会成功,但客户端在处理响应时失败,我得到一个异常

下面是错误的详细信息,谢谢你们提供的帮助

使用Web引用时出错(旧式webservice客户端) 当将服务作为Web引用使用时,我会收到一个
invalidooperationexception
,用于我进行的任何调用,并显示以下消息:

Client found response content type of 'multipart/related; type="application/xop+xml"; boundary="uuid:170e63fa-183c-4b18-9364-c62ca545a6e0"; start="<root.message@cxf.apache.org>"; start-info="text/xml"', but expected 'text/xml'.
The request failed with the error message:
--

--uuid:170e63fa-183c-4b18-9364-c62ca545a6e0
Content-Type: application/xop+xml; charset=UTF-8; type="text/xml";
Content-Transfer-Encoding: binary
Content-ID: <root.message@cxf.apache.org>

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <ns2:openApiConnectionResponse xmlns:ns2="http://api.service.apibatchmember.emailvision.com/" xmlns:ns3="http://exceptions.service.apibatchmember.emailvision.com/">
      <return>DpKTe-9swUeOsxhHH9t-uLPeLyg-aa2xk3-aKe9oJ5S9Yymrnuf1FxYnzpaFojsQSkSCbJsZmrZ_d3v2-7Hj</return>
    </ns2:openApiConnectionResponse>
  </soap:Body>
</soap:Envelope>
--uuid:170e63fa-183c-4b18-9364-c62ca545a6e0--
--.
The content type multipart/related; type="application/xop+xml"; boundary="uuid:af66440a-012e-4444-8814-895c843de5ec"; start="<root.message@cxf.apache.org>"; start-info="text/xml" of the response message does not match the content type of the binding (text/xml; charset=utf-8). If using a custom encoder, be sure that the IsContentTypeSupported method is implemented properly. The first 648 bytes of the response were: '
--uuid:af66440a-012e-4444-8814-895c843de5ec
Content-Type: application/xop+xml; charset=UTF-8; type="text/xml";
Content-Transfer-Encoding: binary
Content-ID: <root.message@cxf.apache.org>

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <ns2:openApiConnectionResponse xmlns:ns2="http://api.service.apibatchmember.emailvision.com/" xmlns:ns3="http://exceptions.service.apibatchmember.emailvision.com/">
      <return>Dqaqb-MJ9V_eplZ8fPh4tdHUbxM-ZtuZsDG6GalAGZSfSzyxgtuuIxZc3aSsnhI4b0SCbJsZmrZ_d3v2-7G8</return>
    </ns2:openApiConnectionResponse>
  </soap:Body>
</soap:Envelope>
--uuid:af66440a-012e-4444-8814-895c843de5ec--'.
就像前面的例子一样;我们得到了一个有效的soap响应,调用成功,但客户端似乎有内容类型问题,并生成了一个异常


我是否可以设置任何选项,以便客户端不会出现响应类型问题?我做过一些谷歌搜索,但到目前为止,我所发现的任何东西都没有帮助我。

对于任何有同样问题的人;我找到了一个将web服务作为服务引用(WCF)使用的解决方案。该属性需要设置为“Mtom”

下面是所需配置设置的一个片段:

<configuration>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding messageEncoding="Mtom">          
        </binding>
      </basicHttpBinding>
    </bindings>
  </system.serviceModel>
</configuration>

编辑:如果自定义绑定存在相同问题,请参阅


我还没有找到一个解决方案,把它当作一个老式的网络参考来使用。

我在相同的功能和相同的公司遇到了完全相同的问题,我花了几个小时在谷歌上试图找到正确的答案。经过这么多的尝试,我终于得到了它。以下是我所做的:以下字符串是从Email Vision返回我的响应(我的主要目的是批量上传,但为了简单和测试,我切换到getLastUpload)

字符串(2180)”

因此,解决方案是尝试从xml文件的开始到开始,从xml文件的结尾到字符串的结尾,去掉字符串的一部分。我发现了一个非常好的脚本,帮助我在这里找到这个解决方案。但是,我的响应xml有点不同,所以我只是稍微修改了该脚本,下面是我的版本

function upload_file_insert_via_soap_obj( $token, $csv_file )
{
    try
    {
        $wsdl_loc = 'https:XXX/apibatchmember/services/BatchMemberService?wsdl';

        $soap_Object = new MTOMSoapClient( $wsdl_loc, array( 'cache_wsdl' => WSDL_CACHE_NONE, 'trace' => true ) );

        $parameters['token'] = $token;
        $parameters['insertUpload']['fileName'] = "insertMemberTest.txt";
        $parameters['insertUpload']['fileEncoding'] = "UTF-8";
        $parameters['insertUpload']['separator'] = ",";
        $parameters['insertUpload']['skipFirstLine'] = "false";
        $parameters['insertUpload']['autoMapping'] = "true"; 

        $parameters['file'] = file_get_contents( "insertMemberTest.txt" );

        $results = $soap_Object->uploadFileInsert( $parameters );

        $upload_id = $results->return;

        echo "<br/>upload_id: ".$upload_id;  
        return $upload_id;  
    }
    catch ( Exception $exception )
    {
        echo "EX REQUEST: " . $soap_Object->__getLastRequest() . "<br/>";
        echo "EX RESPONSE: " . $soap_Object->__getLastResponse() . "<br/>";
        echo "<br/>Response var dump: "; var_dump( $soap_Object->__getLastResponse() ); echo "<br/>";

        echo '<br/><br/> Exception: '.$exception->getMessage()."<br/>";
        var_dump( $exception );
    }
}


/**
* This client extends the ususal SoapClient to handle mtom encoding. Due
* to mtom encoding soap body has test apart from valid xml. This extension
* remove the text and just keeps the response xml.
*/

class MTOMSoapClient extends SoapClient 
{

    public function __doRequest( $request, $location, $action, $version, $one_way = 0 ) 
    {
        $response = parent::__doRequest( $request, $location, $action, $version, $one_way );
        //if resposnse content type is mtom strip away everything but the xml.
        if ( strpos( $response, "Content-Type: application/xop+xml" ) !== false ) 
        {
            //not using stristr function twice because not supported in php 5.2 as shown below
            //$response = stristr(stristr($response, "<s:"), "</s:Envelope>", true) . "</s:Envelope>";
            $tempstr = stristr( $response, "<soap:" );
            $response = substr( $tempstr, 0, strpos( $tempstr, "</soap:Envelope>" ) ) . "</soap:Envelope>";
        }
        //log_message($response);
        return $response;
    }

}
function upload\u file\u insert\u通过soap\u obj($token,$csv\u file)
{
尝试
{
$wsdl_loc='https:XXX/apibatchmember/services/BatchMemberService?wsdl';
$soap\u Object=new MTOMSoapClient($wsdl\u loc,array('cache\u wsdl'=>wsdl\u cache\u NONE,'trace'=>true));
$parameters['token']=$token;
$parameters['insertUpload']['fileName']=“insertMemberTest.txt”;
$parameters['insertUpload']['fileEncoding']=“UTF-8”;
$parameters['insertUpload']['separator']=“,”;
$parameters['insertUpload']['skipFirstLine']=“false”;
$parameters['insertUpload']['autoMapping']=“true”;
$parameters['file']=文件获取内容(“insertMemberTest.txt”);
$results=$soap\u Object->uploadFileInsert($parameters);
$upload\u id=$results->return;
echo“
上传id:.$upload\u id; 返回$upload\u id; } 捕获(异常$Exception) { echo“EX-REQUEST:.$soap\u对象->\uu getLastRequest()。”
; echo“EX-RESPONSE:”..soap_-Object->uu-getLastResponse()。“
”; echo“
响应var dump:;var_dump($soap\u Object->\uu getlastress());echo“
”; 回显“

Exception:”.$Exception->getMessage().“
”; var_dump($例外); } } /** *此客户端扩展了ususal SoapClient以处理mtom编码。到期 *to mtom编码soap正文除了有效的xml之外还有测试 *删除文本,只保留响应xml。 */ 类MTOMSoapClient扩展了SoapClient { 公共函数\u doRequest($request、$location、$action、$version、$one\u way=0) { $response=parent::u doRequest($request、$location、$action、$version、$one\u way); //如果response content type为mtom,则除去xml以外的所有内容。 if(strpos($response,“内容类型:application/xop+xml”)!==false) { //不使用stristr函数两次,因为PHP5.2不支持,如下所示
//$response=stristr(stristr($response,“如果您自行托管WCF服务,并且使用WCF客户端使用该服务。您必须记住在主机中设置
MessageEncoding
属性,如下所示:

BasicHttpBinding binding = new BasicHttpBinding();
binding.MessageEncoding = WSMessageEncoding.Mtom;

我也遇到了这个问题。我的客户端在启动时不断抛出此异常,我无法找出原因,直到我意识到我忘记在主机应用程序中设置绑定
MessageEncoding
属性。

经过几天的努力,我找到了一个非常简单的解决方案:

  • 从VS2010的主菜单中选择工具->WCF服务配置编辑器,激活配置编辑器
  • 再次关闭它
  • 右键点击App.Config文件,找到一个新的菜单项“编辑WCF配置”
  • 点击绑定
  • 将消息编码改为Mtom
  • 保存

  • 我希望这将对某人有所帮助。

    我之所以遇到这个问题,是因为它不是中的第一个元素。

    我也有同样的问题,但与
    有关。要解决它,您可以在绑定下使用单独的
    配置节点配置Mtom消息编码

    <configuration>
      <system.serviceModel>
        <bindings>
          <customBinding>
            <binding name="MyServiceBinding">
              <mtomMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16" messageVersion="Soap12" writeEncoding="utf-8">
                <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
              </mtomMessageEncoding>
            </binding>
          </customBinding>
        </bindings>
      </system.serviceModel>
    </configuration>
    

    我不确定这是否合适,但我很难让我的Windows服务连接到WSDL(使用自动生成的web引用)

    我得到了
    function upload_file_insert_via_soap_obj( $token, $csv_file )
    {
        try
        {
            $wsdl_loc = 'https:XXX/apibatchmember/services/BatchMemberService?wsdl';
    
            $soap_Object = new MTOMSoapClient( $wsdl_loc, array( 'cache_wsdl' => WSDL_CACHE_NONE, 'trace' => true ) );
    
            $parameters['token'] = $token;
            $parameters['insertUpload']['fileName'] = "insertMemberTest.txt";
            $parameters['insertUpload']['fileEncoding'] = "UTF-8";
            $parameters['insertUpload']['separator'] = ",";
            $parameters['insertUpload']['skipFirstLine'] = "false";
            $parameters['insertUpload']['autoMapping'] = "true"; 
    
            $parameters['file'] = file_get_contents( "insertMemberTest.txt" );
    
            $results = $soap_Object->uploadFileInsert( $parameters );
    
            $upload_id = $results->return;
    
            echo "<br/>upload_id: ".$upload_id;  
            return $upload_id;  
        }
        catch ( Exception $exception )
        {
            echo "EX REQUEST: " . $soap_Object->__getLastRequest() . "<br/>";
            echo "EX RESPONSE: " . $soap_Object->__getLastResponse() . "<br/>";
            echo "<br/>Response var dump: "; var_dump( $soap_Object->__getLastResponse() ); echo "<br/>";
    
            echo '<br/><br/> Exception: '.$exception->getMessage()."<br/>";
            var_dump( $exception );
        }
    }
    
    
    /**
    * This client extends the ususal SoapClient to handle mtom encoding. Due
    * to mtom encoding soap body has test apart from valid xml. This extension
    * remove the text and just keeps the response xml.
    */
    
    class MTOMSoapClient extends SoapClient 
    {
    
        public function __doRequest( $request, $location, $action, $version, $one_way = 0 ) 
        {
            $response = parent::__doRequest( $request, $location, $action, $version, $one_way );
            //if resposnse content type is mtom strip away everything but the xml.
            if ( strpos( $response, "Content-Type: application/xop+xml" ) !== false ) 
            {
                //not using stristr function twice because not supported in php 5.2 as shown below
                //$response = stristr(stristr($response, "<s:"), "</s:Envelope>", true) . "</s:Envelope>";
                $tempstr = stristr( $response, "<soap:" );
                $response = substr( $tempstr, 0, strpos( $tempstr, "</soap:Envelope>" ) ) . "</soap:Envelope>";
            }
            //log_message($response);
            return $response;
        }
    
    }
    
    BasicHttpBinding binding = new BasicHttpBinding();
    binding.MessageEncoding = WSMessageEncoding.Mtom;
    
    <configuration>
      <system.serviceModel>
        <bindings>
          <customBinding>
            <binding name="MyServiceBinding">
              <mtomMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16" messageVersion="Soap12" writeEncoding="utf-8">
                <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
              </mtomMessageEncoding>
            </binding>
          </customBinding>
        </bindings>
      </system.serviceModel>
    </configuration>
    
        public DownloadResponse downloadDataFile([System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)] DownloadRequest Request) {
            RequireMtom = true; // ADDED
            object[] results = this.Invoke("downloadDataFile", new object[] {
                        Request});
            RequireMtom = false; // ADDED
            return ((DownloadResponse)(results[0]));
        }