c# - WCF 服务客户端 : The content type text/html; charset=utf-8 of the response message does not match the content type of the binding

标签 c# .net wcf web-services

我在我的本地 IIS 服务器上运行了一个 WCF 服务。我已将它添加为 C# 网站项目的服务引用,它添加得很好并自动生成代理类。

但是,当我尝试调用任何服务契约(Contract)时,出现以下错误:

描述 当前网络请求的执行。 请查看堆栈跟踪以获取更多信息 有关错误及其位置的信息 它起源于代码。

异常详细信息: System.ServiceModel.ProtocolException: 内容类型 text/html; 响应消息的 charset=utf-8 不匹配的内容类型 绑定(bind)(应用程序/soap+xml; 字符集=utf-8)。如果使用自定义 编码器,请确保 IsContentTypeSupported 方法是 实现得当。第一个1024 响应的字节是:'

功能 bredir(d,u,r,v,c){var w,h,wd,hd,bi;var b=假;变量 p=假;变量 s=[[300,250,false],[250,250,false],[240,400,false],[336,280,false],[180,150,false],[468,60,false],[234,60,false],[ 88,31,假],[120,90,假],[120,60,假],[120,240,假],[125,125,假],[728,90,假],[160,600,假],[ 120,600,false],[300,600,false],[300,125,false],[530,300,false],[190,200,false],[470,250,false],[720,300,true],[500,350,true],[550,480, true]];if(typeof(window.innerHeight)=='number'){h=window.innerHeight;w=window.innerWidth;}else if(typeof(document.body.offsetHeight)=='number'){h=document.body.offsetHeight;w=document.body.offsetWidth;}for(var 我=0;我

我还有一个控制台应用程序,它也与 WCF 服务通信,并且控制台应用程序能够正常调用方法而不会出现此错误。

以下是我的配置文件的摘录。

WCF 服务 Web.Config:

<system.serviceModel>
   <services>
      <service name="ScraperService" behaviorConfiguration="ScraperServiceBehavior">
         <endpoint address=""
                   binding="wsHttpBinding" 
                   bindingConfiguration="WSHttpBinding_IScraperService"
                   contract="IScraperService" />
         <endpoint address="mex" 
                   binding="mexHttpBinding" 
                   contract="IMetadataExchange" />
         <host>
            <baseAddresses>
                <add baseAddress="http://example.com" />
            </baseAddresses>
         </host>
      </service>
   </services>
   <bindings>
       <wsHttpBinding>
           <binding name="WSHttpBinding_IScraperService"
                    bypassProxyOnLocal="false" transactionFlow="false"
                    hostNameComparisonMode="StrongWildcard"
                    maxBufferPoolSize="2000000" maxReceivedMessageSize="2000000"
                    messageEncoding="Text" textEncoding="utf-8"
                    useDefaultWebProxy="true" allowCookies="false">
               <readerQuotas 
                     maxDepth="2000000" maxStringContentLength="2000000" 
                     maxArrayLength="2000000" maxBytesPerRead="2000000"
                     maxNameTableCharCount="2000000" />
               <reliableSession 
                     enabled="false" ordered="true" inactivityTimeout="00:10:00" />
               <security mode="Message">
                   <message clientCredentialType="Windows"
                            negotiateServiceCredential="true"
                            algorithmSuite="Default"
                            establishSecurityContext="true" />
               </security>
            </binding>
          </wsHttpBinding>
      </bindings>
      <behaviors>
          <serviceBehaviors>
              <behavior name="ScraperServiceBehavior">
                  <serviceMetadata httpGetEnabled="true" />
                  <serviceDebug includeExceptionDetailInFaults="true" />
              </behavior>
          </serviceBehaviors>
     </behaviors>
</system.serviceModel>

网站项目服务客户端Web.Config:

<system.serviceModel>
   <bindings>
      <wsHttpBinding>
          <binding name="WSHttpBinding_IScraperService" 
              closeTimeout="00:01:00" openTimeout="00:01:00" 
              receiveTimeout="00:10:00" sendTimeout="00:01:00"
              bypassProxyOnLocal="false" transactionFlow="false" 
              hostNameComparisonMode="StrongWildcard"
              maxBufferPoolSize="524288" maxReceivedMessageSize="65536" 
              messageEncoding="Text" textEncoding="utf-8"
              useDefaultWebProxy="true" allowCookies="false">
              <readerQuotas 
                  maxDepth="32" maxStringContentLength="8192" 
                  maxArrayLength="16384" maxBytesPerRead="4096" 
                  maxNameTableCharCount="16384" />
              <reliableSession enabled="false"
                  ordered="true" inactivityTimeout="00:10:00" />
              <security mode="Message">
                  <transport clientCredentialType="Windows" 
                       proxyCredentialType="None" realm="" />
                  <message clientCredentialType="Windows" 
                       negotiateServiceCredential="true"
                       algorithmSuite="Default" />
              </security>
          </binding>
       </wsHttpBinding>
    </bindings>
<client>
        <endpoint name="WSHttpBinding_IScraperService"
            address="http://example.com/ScraperService.svc"
            binding="wsHttpBinding" 
            bindingConfiguration="WSHttpBinding_IScraperService"
            contract="ScraperService.IScraperService" >
           <identity>
               <servicePrincipalName value="host/FreshNET-II" />
           </identity>
        </endpoint>
     </client>
</system.serviceModel>

最佳答案

我遇到了类似的问题。我通过更改解决了它

<basicHttpBinding>

<basicHttpsBinding>

并且还更改了我的 URL 以使用 https://而不是 http://。

同样在节点,改变

binding="basicHttpBinding" 

binding="basicHttpsBinding"

这成功了。

关于c# - WCF 服务客户端 : The content type text/html; charset=utf-8 of the response message does not match the content type of the binding,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5243929/

相关文章:

c# - 在哪里处理 DTO<-> 业务对象转换

c# - C#中的 'int?'和 'int'有什么区别?

c# - MEF错误 "Only one batch can be composed at a time"

c# - 如何从 IUnknown_QueryService SID_SExplorerBrowserFrame 检索 C# 中的 IFileOpenDialog 接口(interface)?

c# - Expression.Call 中的 ToUpper()

c# - 在 C# 中选择数组中的一系列项目

.net - 构建热插拔WCF服务库的最佳方式

wcf - netstat 说 443 已打开,但我无法使用 telnet 连接到它。为什么?

c# - C# 是静态类型还是 moSTLy 静态类型?

.net - 如何同时更新两个字典