我正在尝试开发一个Web应用程序,使用WCF向Web服务提交一个简单的登录请求,但我不断收到“内容类型文本/响应消息的明文不匹配”错误,即使Web服务似乎正在返回有效的XML.
我已经回顾了几十个类似的S.O.有关此错误的帖子并排除了更常见的可能性:
>我可以在响应中看到它不是一个错误页面回来(如here所述).
>我似乎没有绑定不匹配问题(如here所述).我的应用程序和
Web服务都使用SOAP 1.1,我指定了“basicHttpBinding”.
>我的错误消息中包含的响应似乎显示有效的XML
包括我想要的用户和响应变量.
>我可以在Web服务日志文件中看到登录方法
完全执行而不抛出异常.
>我将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.
这是绑定在我的Web.Config中的样子:
binding="basicHttpBinding" bindingConfiguration="AuthenticationServiceSoapBinding"
contract="MyNamespace.AuthenticationService" name="AuthenticationServiceImplPort" />
这是我的错误信息:
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: ‘
xxxx
https://MyWebApplicationURL
xxxx
xxxx
xxxx
true
xxxx
xxxx
xxxx
xxxx
‘.
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
以下是我在WebS服务中对WireShark的回应:
解决方法:
问题是当服务响应text / xml或application / xml时,服务正在使用text / plain进行响应,而这正是您的WCF绑定所期望的,以便正确处理它.从根本上说,这是服务方面的“问题”:它们返回XML但是将其称为明文,这在技术上是准确的,但不像XML内容类型那样具体.在WCF方面,内置协议处理相当严格,并且期望内容类型为text / xml或application / xml,以便继续对其进行反序列化.
您可以通过支持text / plain的writing a custom WCF encoder阻止其内容类型问题,但将其解析为XML.
标签:c,asp-net,wcf,web-services
来源: https://codeday.me/bug/20190517/1120534.html