响应消息的内容类型 text/xml; charset=ISO-8859-1 与绑定(text/xml; charset=utf-8)的内容类型不匹配。如果使用

时间:2021-12-16 09:16:14
用c#测试个合作伙伴公司的webservice(php编写的),测试代码:
protected void Page_Load(object sender, EventArgs e)
    {
        bzezt.getmesxmlPortTypeClient ptc = new bzezt.getmesxmlPortTypeClient();
        string rtn = ptc.getmesxml("test", MD5Encoding("888888"));
    }

    public static string MD5Encoding(string rawPass)
    {
        // 创建MD5类的默认实例:MD5CryptoServiceProvider
        MD5 md5 = MD5.Create();
        byte[] bs = Encoding.UTF8.GetBytes(rawPass);
        byte[] hs = md5.ComputeHash(bs);
        StringBuilder sb = new StringBuilder();
        foreach (byte b in hs)
        {
            // 以十六进制格式格式化
            sb.Append(b.ToString("x2"));
        }
        return sb.ToString();
    }

报如下错误:
“/WebSite3”应用程序中的服务器错误。

响应消息的内容类型 text/xml; charset=ISO-8859-1 与绑定(text/xml; charset=utf-8)的内容类型不匹配。如果使用自定义编码器,请确保正确实现 IsContentTypeSupported 方法。响应的前 1024 个字节为:“<?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><ns1:getmesxmlResponse xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/"><return xsi:type="xsd:string">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;Request&gt;
&lt;scenery&gt;
&lt;scn_state&gt;1&lt;/scn_state&gt;
&lt;sid&gt;955&lt;/sid&gt;
&lt;uid&gt;2007359&lt;/uid&gt;
&lt;name&gt;抚顺皇家海洋乐园&lt;/name&gt;
&lt;provineid&gt;18&lt;/provineid&gt;
&lt;cityid&gt;244&lt;/cityid&gt;
&lt;areaid&gt;-22&lt;/areaid&gt;
&lt;address&gt;辽宁省抚顺市高湾开发区高阳路3号&lt;/address&gt;
&lt;info&gt;%3Cp%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B+%E7%9A%87%E5%AE%B6%E6%B5%B7%E6%B4%8B%E4%B9%90%E5%9B%AD%EF%BC%8C%E7%9B%AE”。

说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 

异常详细信息: System.ServiceModel.ProtocolException: 响应消息的内容类型 text/xml; charset=ISO-8859-1 与绑定(text/xml; charset=utf-8)的内容类型不匹配。如果使用自定义编码器,请确保正确实现 IsContentTypeSupported 方法。响应的前 1024 个字节为:“<?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><ns1:getmesxmlResponse xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/"><return xsi:type="xsd:string">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;Request&gt;
&lt;scenery&gt;
&lt;scn_state&gt;1&lt;/scn_state&gt;
&lt;sid&gt;955&lt;/sid&gt;
&lt;uid&gt;2007359&lt;/uid&gt;
&lt;name&gt;抚顺皇家海洋乐园&lt;/name&gt;
&lt;provineid&gt;18&lt;/provineid&gt;
&lt;cityid&gt;244&lt;/cityid&gt;
&lt;areaid&gt;-22&lt;/areaid&gt;
&lt;address&gt;辽宁省抚顺市高湾开发区高阳路3号&lt;/address&gt;
&lt;info&gt;%3Cp%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B+%E7%9A%87%E5%AE%B6%E6%B5%B7%E6%B4%8B%E4%B9%90%E5%9B%AD%EF%BC%8C%E7%9B%AE”。

源错误: 


行 89:         [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
行 90:         bzezt.getmesxmlResponse bzezt.getmesxmlPortType.getmesxml(bzezt.getmesxmlRequest request) {
行 91:             return base.Channel.getmesxml(request);
行 92:         }
行 93:         

源文件: c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\website3\a307838f\3c9d09e4\App_WebReferences.ns_ixokz.0.cs    行: 91 

堆栈跟踪: 


[ProtocolException: 响应消息的内容类型 text/xml; charset=ISO-8859-1 与绑定(text/xml; charset=utf-8)的内容类型不匹配。如果使用自定义编码器,请确保正确实现 IsContentTypeSupported 方法。响应的前 1024 个字节为:“<?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><ns1:getmesxmlResponse xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/"><return xsi:type="xsd:string">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;Request&gt;
&lt;scenery&gt;
&lt;scn_state&gt;1&lt;/scn_state&gt;
&lt;sid&gt;955&lt;/sid&gt;
&lt;uid&gt;2007359&lt;/uid&gt;
&lt;name&gt;抚顺皇家海洋乐园&lt;/name&gt;
&lt;provineid&gt;18&lt;/provineid&gt;
&lt;cityid&gt;244&lt;/cityid&gt;
&lt;areaid&gt;-22&lt;/areaid&gt;
&lt;address&gt;辽宁省抚顺市高湾开发区高阳路3号&lt;/address&gt;
&lt;info&gt;%3Cp%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B+%E7%9A%87%E5%AE%B6%E6%B5%B7%E6%B4%8B%E4%B9%90%E5%9B%AD%EF%BC%8C%E7%9B%AE”。]
   System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) +9442991
   System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) +345
   bzezt.getmesxmlPortType.getmesxml(getmesxmlRequest request) +0
   bzezt.getmesxmlPortTypeClient.bzezt.getmesxmlPortType.getmesxml(getmesxmlRequest request) in c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\website3\a307838f\3c9d09e4\App_WebReferences.ns_ixokz.0.cs:91
   bzezt.getmesxmlPortTypeClient.getmesxml(String uname, String pwd) in c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\website3\a307838f\3c9d09e4\App_WebReferences.ns_ixokz.0.cs:98
   _Default.Page_Load(Object sender, EventArgs e) in c:\Users\Administrator\Documents\Visual Studio 2010\WebSites\WebSite3\Default.aspx.cs:16
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
   System.Web.UI.Control.OnLoad(EventArgs e) +91
   System.Web.UI.Control.LoadRecursive() +74
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207

版本信息: Microsoft .NET Framework 版本:4.0.30319; ASP.NET 版本:4.0.30319.1022



搞了一上午了,还没解决。尴尬!求高手

6 个解决方案

#1


在config文件修改binding
<bindings>
  <basicHttpBinding>
    <binding name="YourBinding" textEncoding="ISO-8859-1" />
  </basicHttpBinding>
</bindings>

#2


谢谢回复!
上午试过的,又试了试,报该编码不受支持的错:

“/WebSite3”应用程序中的服务器错误。

文本消息格式中使用的文本编码“iso-8859-1”不受支持。
参数名: encoding

说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 

异常详细信息: System.ArgumentException: 文本消息格式中使用的文本编码“iso-8859-1”不受支持。
参数名: encoding

源错误: 


行 68:     public partial class getmesxmlPortTypeClient : System.ServiceModel.ClientBase<bzezt.getmesxmlPortType>, bzezt.getmesxmlPortType {
行 69:         
行 70:         public getmesxmlPortTypeClient() {
行 71:         }
行 72:         

源文件: c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\website3\a307838f\3c9d09e4\App_WebReferences.ns_ixokz.0.cs    行: 70 

堆栈跟踪: 


[ArgumentException: 文本消息格式中使用的文本编码“iso-8859-1”不受支持。
参数名: encoding]
   System.ServiceModel.Channels.TextEncoderDefaults.ValidateEncoding(Encoding encoding) +12342059
   System.ServiceModel.Channels.TextMessageEncodingBindingElement.set_WriteEncoding(Encoding value) +23
   System.ServiceModel.Configuration.BasicHttpBindingElement.OnApplyConfiguration(Binding binding) +228
   System.ServiceModel.Configuration.StandardBindingElement.ApplyConfiguration(Binding binding) +167
   System.ServiceModel.Description.ConfigLoader.LookupBinding(String bindingSectionName, String configurationName, BindingCollectionElement bindingCollectionElement, Binding defaultBinding) +359
   System.ServiceModel.Description.ConfigLoader.LookupBinding(String bindingSectionName, String configurationName, ContextInformation context) +56
   System.ServiceModel.Description.ConfigLoader.LoadChannelBehaviors(ServiceEndpoint serviceEndpoint, String configurationName) +150
   System.ServiceModel.ChannelFactory.ApplyConfiguration(String configurationName, Configuration configuration) +124
   System.ServiceModel.ChannelFactory.ApplyConfiguration(String configurationName) +6
   System.ServiceModel.ChannelFactory.InitializeEndpoint(String configurationName, EndpointAddress address) +138
   System.ServiceModel.ChannelFactory`1..ctor(String endpointConfigurationName, EndpointAddress remoteAddress) +138
   System.ServiceModel.EndpointTrait`1.CreateSimplexFactory() +12274587
   System.ServiceModel.ClientBase`1.CreateChannelFactoryRef(EndpointTrait`1 endpointTrait) +32
   System.ServiceModel.ClientBase`1.InitializeChannelFactoryRef() +328
   System.ServiceModel.ClientBase`1..ctor() +116
   bzezt.getmesxmlPortTypeClient..ctor() in c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\website3\a307838f\3c9d09e4\App_WebReferences.ns_ixokz.0.cs:70
   _Default.Page_Load(Object sender, EventArgs e) in c:\Users\Administrator\Documents\Visual Studio 2010\WebSites\WebSite3\Default.aspx.cs:15
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
   System.Web.UI.Control.OnLoad(EventArgs e) +91
   System.Web.UI.Control.LoadRecursive() +74
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207

版本信息: Microsoft .NET Framework 版本:4.0.30319; ASP.NET 版本:4.0.30319.1022

#3


http://*.com/questions/1908030/calling-a-webservice-that-uses-iso-8859-1-encoding-from-wcf

#4


谢谢!试着在config文件里添点代码,还是不行。在让对方看看为什么编写这个webservice时encoding设置成iso-8859-1。

#5


楼主是需要自定义格式吗?

我所知道的绑定格式:

   <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="UB_Soap_Internet_Payments_InterfaceBinding" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                    useDefaultWebProxy="true">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <security mode="None">
                        <transport clientCredentialType="None" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="UserName" algorithmSuite="Default" />
                    </security>
                </binding>
            </basicHttpBinding>
            <customBinding>
                <binding name="DefaultBinding" >
                    <customTextMessageEncoding encoding="ISO-8859-1"/>
                    <httpTransport />
                </binding>

                <binding name="SecureBinding" >
                    <customTextMessageEncoding encoding="ISO-8859-1"/>
                    <security>
                        <secureConversationBootstrap />
                    </security>
                    <httpTransport />
                </binding>
            </customBinding>
        </bindings>
        <client>
            <endpoint name="default" address="client's ip"
                binding="custombinding" bindingConfiguration="defaultbinding"
                contract="Microsoft.ServiceModel.Samples.ICalculator"/>
        </client>
        <extensions>
            <bindingElementExtensions>
                <add name="customTextMessageEncoding" type="Microsoft.ServiceModel.Samples.CustomTextMessageEncodingElement, CustomTextMessageEncoder" />
            </bindingElementExtensions>
        </extensions>
    </system.serviceModel>

#6


9个月前的事了,记得后来用了2.0的framework就没遇到问题

#1


在config文件修改binding
<bindings>
  <basicHttpBinding>
    <binding name="YourBinding" textEncoding="ISO-8859-1" />
  </basicHttpBinding>
</bindings>

#2


谢谢回复!
上午试过的,又试了试,报该编码不受支持的错:

“/WebSite3”应用程序中的服务器错误。

文本消息格式中使用的文本编码“iso-8859-1”不受支持。
参数名: encoding

说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 

异常详细信息: System.ArgumentException: 文本消息格式中使用的文本编码“iso-8859-1”不受支持。
参数名: encoding

源错误: 


行 68:     public partial class getmesxmlPortTypeClient : System.ServiceModel.ClientBase<bzezt.getmesxmlPortType>, bzezt.getmesxmlPortType {
行 69:         
行 70:         public getmesxmlPortTypeClient() {
行 71:         }
行 72:         

源文件: c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\website3\a307838f\3c9d09e4\App_WebReferences.ns_ixokz.0.cs    行: 70 

堆栈跟踪: 


[ArgumentException: 文本消息格式中使用的文本编码“iso-8859-1”不受支持。
参数名: encoding]
   System.ServiceModel.Channels.TextEncoderDefaults.ValidateEncoding(Encoding encoding) +12342059
   System.ServiceModel.Channels.TextMessageEncodingBindingElement.set_WriteEncoding(Encoding value) +23
   System.ServiceModel.Configuration.BasicHttpBindingElement.OnApplyConfiguration(Binding binding) +228
   System.ServiceModel.Configuration.StandardBindingElement.ApplyConfiguration(Binding binding) +167
   System.ServiceModel.Description.ConfigLoader.LookupBinding(String bindingSectionName, String configurationName, BindingCollectionElement bindingCollectionElement, Binding defaultBinding) +359
   System.ServiceModel.Description.ConfigLoader.LookupBinding(String bindingSectionName, String configurationName, ContextInformation context) +56
   System.ServiceModel.Description.ConfigLoader.LoadChannelBehaviors(ServiceEndpoint serviceEndpoint, String configurationName) +150
   System.ServiceModel.ChannelFactory.ApplyConfiguration(String configurationName, Configuration configuration) +124
   System.ServiceModel.ChannelFactory.ApplyConfiguration(String configurationName) +6
   System.ServiceModel.ChannelFactory.InitializeEndpoint(String configurationName, EndpointAddress address) +138
   System.ServiceModel.ChannelFactory`1..ctor(String endpointConfigurationName, EndpointAddress remoteAddress) +138
   System.ServiceModel.EndpointTrait`1.CreateSimplexFactory() +12274587
   System.ServiceModel.ClientBase`1.CreateChannelFactoryRef(EndpointTrait`1 endpointTrait) +32
   System.ServiceModel.ClientBase`1.InitializeChannelFactoryRef() +328
   System.ServiceModel.ClientBase`1..ctor() +116
   bzezt.getmesxmlPortTypeClient..ctor() in c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\website3\a307838f\3c9d09e4\App_WebReferences.ns_ixokz.0.cs:70
   _Default.Page_Load(Object sender, EventArgs e) in c:\Users\Administrator\Documents\Visual Studio 2010\WebSites\WebSite3\Default.aspx.cs:15
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
   System.Web.UI.Control.OnLoad(EventArgs e) +91
   System.Web.UI.Control.LoadRecursive() +74
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207

版本信息: Microsoft .NET Framework 版本:4.0.30319; ASP.NET 版本:4.0.30319.1022

#3


http://*.com/questions/1908030/calling-a-webservice-that-uses-iso-8859-1-encoding-from-wcf

#4


谢谢!试着在config文件里添点代码,还是不行。在让对方看看为什么编写这个webservice时encoding设置成iso-8859-1。

#5


楼主是需要自定义格式吗?

我所知道的绑定格式:

   <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="UB_Soap_Internet_Payments_InterfaceBinding" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                    useDefaultWebProxy="true">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <security mode="None">
                        <transport clientCredentialType="None" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="UserName" algorithmSuite="Default" />
                    </security>
                </binding>
            </basicHttpBinding>
            <customBinding>
                <binding name="DefaultBinding" >
                    <customTextMessageEncoding encoding="ISO-8859-1"/>
                    <httpTransport />
                </binding>

                <binding name="SecureBinding" >
                    <customTextMessageEncoding encoding="ISO-8859-1"/>
                    <security>
                        <secureConversationBootstrap />
                    </security>
                    <httpTransport />
                </binding>
            </customBinding>
        </bindings>
        <client>
            <endpoint name="default" address="client's ip"
                binding="custombinding" bindingConfiguration="defaultbinding"
                contract="Microsoft.ServiceModel.Samples.ICalculator"/>
        </client>
        <extensions>
            <bindingElementExtensions>
                <add name="customTextMessageEncoding" type="Microsoft.ServiceModel.Samples.CustomTextMessageEncodingElement, CustomTextMessageEncoder" />
            </bindingElementExtensions>
        </extensions>
    </system.serviceModel>

#6


9个月前的事了,记得后来用了2.0的framework就没遇到问题