问题描述:
远程服务器返回错误: (415) Cannot process the message because the content type 'application/soap+xml; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'.。
针对这个问题已经百度尝试过,但还没有解决,希望知道的朋友给出解决方案或意见。谢谢。
服务端代码:
<system.serviceModel> <services> <service name="UserApplicationService" behaviorConfiguration="CustomeBehavior"> <endpoint address="" binding="wsHttpBinding" contract="UserApplicationService" bindingConfiguration="CustomWsHttpBinding" /> </service> </services> <bindings> <wsHttpBinding> <binding name="CustomWsHttpBinding"> <security mode="Message"> <message clientCredentialType="Certificate" /> </security> </binding> </wsHttpBinding> </bindings> <behaviors> <serviceBehaviors> <behavior name="CustomeBehavior"> <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="true"/> <useRequestHeadersForMetadataAddress> <defaultPorts> <add scheme="http" port="8002" /> <add scheme="https" port="8002" /> </defaultPorts> </useRequestHeadersForMetadataAddress> <serviceCredentials> <clientCertificate> <authentication certificateValidationMode="None"/> </clientCertificate> <serviceCertificate findValue="ZZGJServer" storeLocation="LocalMachine" storeName="My" x509FindType="FindBySubjectName" /> </serviceCredentials> </behavior> <behavior name=""> <!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false --> <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/> <!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 --> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> </behaviors> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> </system.serviceModel> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> <!-- 若要在调试过程中浏览 Web 应用程序根目录,请将下面的值设置为 True。 在部署之前将该值设置为 False 可避免泄露 Web 应用程序文件夹信息。 --> <directoryBrowse enabled="true"/> </system.webServer>
客户端代码:
<system.serviceModel> <client> <endpoint address="http://localhost:13164/UserApplicationService.svc" behaviorConfiguration="CustomeBehavior" binding="wsHttpBinding" bindingConfiguration="CustomWsHttpBinding" contract="UserApplicationRemoteService.IUserApplicationService" name="BasicHttpBinding_IUserApplicationService" /> </client> <bindings> <basicHttpBinding> <binding name="BasicHttpBinding_IUserApplicationService" /> </basicHttpBinding> <wsHttpBinding> <binding name="CustomWsHttpBinding" messageEncoding="Text" textEncoding="utf-8"> <security mode="None"> <message clientCredentialType="Certificate" /> </security> </binding> </wsHttpBinding> </bindings> <behaviors> <endpointBehaviors> <behavior name="CustomeBehavior"> <clientCredentials> <clientCertificate findValue="ZZGJClient" storeLocation="LocalMachine" storeName="My" x509FindType="FindBySubjectName" /> <serviceCertificate> <authentication certificateValidationMode="None"/> </serviceCertificate> </clientCredentials> </behavior> </endpointBehaviors> </behaviors> </system.serviceModel>
现在判定是服务端配置错误,service里的name属性没有写命名空间,客户端引用后绑定方式还是BasicHttpBinding(自己手动改写是无效的),所以导致以上错误。
解决过程中,自己认为可以分为两部分。一是使用wsHttpBinding绑定方式使数据传输过程中加密,二是X509证书作为一个访问该站点的凭证,更加安全。