c# - 收到的 Web 服务 XML 响应为 "text/plain"。 (又一个内容类型错误。)

标签 c# asp.net wcf web-services

我正在尝试开发一个 Web 应用程序,该应用程序使用 WCF 向 Web 服务提交一个简单的登录请求,但我不断收到“响应消息的内容类型文本/纯文本不匹配”错误,即使 Web 服务出现返回有效的 XML。

我已经查看了数十个类似的 S.O.关于此错误的帖子并排除了更常见的可能性:

  1. 我可以在响应中看到返回的不是错误页面(如讨论的 here )。
  2. 我似乎没有绑定(bind)不匹配问题(如讨论的 here )。我的应用程序和 Web 服务都使用 SOAP 1.1,并且我指定了“basicHttpBinding”。
  3. 我的错误消息中包含的响应似乎显示了有效的 XML 其中包括我想要的用户和响应变量。
  4. 我可以在 Web 服务日志文件中看到登录方法是 完全执行而不抛出异常。
  5. 我将 XML 响应粘贴到 W3Schools 验证器中,没有 错误。

还有什么可能导致我的错误,为什么这不被识别为有效的 XML?

我的应用程序使用 .Net 4.0 和 Visual Studio 2010,它与单独服务器上的 Java/Tomcat Web 服务通信。

以下是我的登录代码的相关摘录:

AuthenticationServiceClient client = new AuthenticationServiceClient(strPortName, strURL);
client.Open();
UserCredentials credentials = new UserCredentials();
credentials.userName = TestUsername;
credentials.password = TestPassword;
LoginResponse response = client.login(credentials); // Using WCF.

这是绑定(bind)在我的 Web.Config 中的样子:

<system.serviceModel>
  <bindings>
    <basicHttpBinding>
      <binding name="AuthenticationServiceSoapBinding" />
    </basicHttpBinding>
  </bindings>
  <client>
    <endpoint address="http://mywebserviceurl"
      binding="basicHttpBinding" bindingConfiguration="AuthenticationServiceSoapBinding"
      contract="MyNamespace.AuthenticationService" name="AuthenticationServiceImplPort" />
  </client>
</system.serviceModel>

这是我的错误信息:

Error testing connection to web service at "WebServiceURL": System.ServiceModel.ProtocolException: The content type text/plain 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 788 bytes of the response were: '

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<loginReponse xmlns="urn:xxxx">
<authKey>xxxx</authKey>
<authMetadata>
<IPaddress>https://MyWebApplicationURL</IPaddress>
<loginTimestamp>xxxx</loginTimestamp><levelOfAssurance>
<AuthMechanism>xxxx</AuthMechanism>
<VettingAssertion>xxxx</VettingAssertion>
</levelOfAssurance>
</authMetadata>
<errorText></errorText>
<successfulLoginFlag>true</successfulLoginFlag>
<userSummary>
<GUID>xxxx</GUID>
<commonName>xxxx</commonName>
<loginName>xxxx</loginName>
</userSummary>
<passwordExpiryDays>xxxx</passwordExpiryDays></loginReponse>
</soap:Body>
</soap:Envelope>

'.

Server stack trace: at System.ServiceModel.Channels.HttpChannelUtilities.ValidateRequestReplyResponse(HttpWebRequest request, HttpWebResponse response, HttpChannelFactory factory, WebException responseException, ChannelBinding channelBinding) at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout) at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout) at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout) at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation) at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]: at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) at MyNamespace.AuthenticationService.AuthenticationService.login(loginRequest request) at MyNamespace.AuthenticationService.AuthenticationServiceClient.MyNamespace.AuthenticationService.AuthenticationService.login(loginRequest request) in C:...\AuthenticationService\Reference.cs:line 1716 at MyNamespace.AuthenticationService.AuthenticationServiceClient.login(UserCredentials credentials) in C:...\AuthenticationService\Reference.cs:line 1722
at MyNamespace.Forms.TestWebService.TestLogin_Click(Object sender, EventArgs e) in C:...\TestWebService.aspx.cs:line 284

这是我在 WireShark 中对 Web 服务的响应:

WireShark Screenshot

最佳答案

问题是服务在应该用 text/xmlapplication/xml 响应时用 text/plain 响应,这是您的 WCF 绑定(bind)为了正确处理它所期望的。从根本上说,这是服务端的一个“问题”:他们返回 XML 但称其为纯文本,这在技术上是准确的,但不像 XML 内容类型那样具体。在 WCF 端,内置协议(protocol)处理相当严格,并且期望内容类型为 text/xmlapplication/xml 以便继续反序列化。

您可以通过 writing a custom WCF encoder 来阻止他们的内容类型问题 支持 text/plain 但将其解析为 XML。

关于c# - 收到的 Web 服务 XML 响应为 "text/plain"。 (又一个内容类型错误。),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21313090/

相关文章:

c# - Javascript getElementbyId 工作一次但不能工作两次?

c# - 提供程序命名管道提供程序错误 40 无法打开到 SQL Server 错误 2 的连接

javascript - 如何使用javascript显示和隐藏按钮?

c# - 要在 C# 中使用 RESTful Web 服务,我应该使用 HttpWebRequest 吗?

c# - wsdl.exe 生成的 WCF 服务在 wsdl 中公开私有(private)字段

c# - 嵌入html、javascript、css作为资源并在web控件中使用

asp.net - 如何实现 "Don' t 再次显示此消息”

android - Visual Studio 和手机开发

c# - EndpointDispatcher 的 ContractFilter 不匹配(错误处理)

c# - NHibernate - TransformUsing.SingleOrDefault 返回默认值