Web服务XML响应作为"text/plain"接收.(还有另一种内容类型错误.)

Mac*_*Mac 5 c# asp.net wcf web-services

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

我已经回顾了有关此错误的数十篇类似的SO帖子,并排除了更常见的可能性:

  1. 我可以在响应中看到它不是一个Error页面(如这里所讨论的).
  2. 我似乎不具有绑定不匹配的问题(如讨论这里).我的应用程序和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.
Run Code Online (Sandbox Code Playgroud)

这是绑定在我的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>
Run Code Online (Sandbox Code Playgroud)

这是我的错误信息:

在"WebServiceURL"上测试与Web服务的连接时出错:System.ServiceModel.ProtocolException:响应消息的内容类型text/plain与绑定的内容类型(text/xml; charset = utf-8)不匹配.如果使用自定义编码器,请确保正确实现IsContentTypeSupported方法.响应的前788个字节是:'

<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>
Run Code Online (Sandbox Code Playgroud)

".

服务器堆栈跟踪:位于系统的System.ServiceModel.Channels.HttpChannelUtilities.ValidateRequestReplyResponse(HttpWebRequest请求,HttpWebResponse响应,HttpChannelFactory工厂,WebException responseException,ChannelBinding channelBinding)处于系统的System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan超时)系统中的System.ServiceModel.Channels.ServiceChannel.Call(String action,Boolean oneway,ProxyOperationRuntime操作,Object [] ins,Object [] outs,TimeSpan timeout)中的.ServiceModel.Channels.RequestChannel.Request(消息消息,TimeSpan超时) System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)中的.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall,ProxyOperationRuntime操作)

在[0]处重新抛出异常:位于My.space.AuthenticationService的System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&msgData,Int32类型)的System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg,IMessage retMsg). Authentication.login(loginRequest request)在MyNamespace.AuthenticationService.AuthenticationServiceClient.MyNamespace.AuthenticationService.AuthenticationService.login(loginRequest request)中的C:...\AuthenticationService\Reference.cs:第1716行,位于MyNamespace.AuthenticationService.AuthenticationServiceClient.login(UserCredentials) C:...\AuthenticationService\Reference.cs中的凭证):位于C:...\
TestWebService.aspx.cs中的MyNamespace.Forms.TestWebService.TestLogin_Click(Object sender,EventArgs e)的第1722 行:第284行

以下是我在WebS服务中对WireShark的回应:

WireShark截图

Han*_*ney 6

问题是服务正在响应text/plain它应该响应的时间text/xmlapplication/xmlWCF绑定所期望的,以便正确处理它.从根本上说,这是服务方面的"问题":它们返回XML但是将其称为明文,这在技术上是准确的,但不像XML内容类型那样具体.在WCF方面,内置协议处理相当严格,并期望内容类型为text/xmlapplication/xml继续进行反序列化.

您可以通过编写支持text/plain但将其解析为XML 的自定义WCF编码器来阻止其内容类型问题.


归档时间:

查看次数:

10291 次

最近记录:

11 年,8 月 前