我们需要从C#/。Net 4.0应用程序中使用使用Java开发的SOAP 1.1 Web服务。我们无权访问服务程序或其所在的服务器。该服务通过非SSL连接公开,并且需要WS-Security,Username / PasswordText。
使用SoapUI(http://soapui.org/),我们可以通过创建项目,将其指向WSDL并设置简单的Username / PasswordText WS-Security配置来使用Web服务。
问题是我们无法使用WCF使用Web服务。经过一些研究,我们发现了一些信息,使我们相信该问题可能是SOAP 1.1,WS-Security和wsHttpBinding之间的不兼容。使用的C#代码如下:
//The WSService.ServiceClient class is generated by the Service Reference
var client = new WSService.ServiceClient();
client.ClientCredentials.UserName.UserName = "Username";
client.ClientCredentials.UserName.Password = "Password";
var response = client.MethodToBeConsumed();
创建的绑定如下:
<binding name="GeneratedBinding" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
服务参考创建的绑定是basicHttpBinding。我们了解需要为Web服务安全性(SOAP 1.2)使用wsHttpBinding,但是由于服务使用SOAP 1.1,所以这两个似乎不兼容。
正确的请求消息应该是(由SOAPUI生成):
POST http://server:port/Service HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: text/xml;charset=UTF-8
SOAPAction: ""
Authorization: Basic ENCRYPTEDPASSWORD
User-Agent: Jakarta Commons-HttpClient/3.1
Host: server:port
Content-Length: 324
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:service.version.details">
<soapenv:Header/>
<soapenv:Body>
<urn:methodName>
<arg0>1</arg0>
<arg1>1</arg1>
</urn:method>
</soapenv:Body>
</soapenv:Envelope>
使用WCF真的没有办法实现这一目标吗?可以使用customBinding解决吗?我们是手工制作标题/消息并解析响应的唯一选择吗?我是否需要让Web服务开发人员通过非SSL连接使用WS-Security和SOAP 1.1?
最佳答案
最终的解决方案是将Authorization标头插入请求,如以下文章所述:
http://social.msdn.microsoft.com/Forums/is/wcf/thread/4f8ab001-dafa-4347-bc41-95255ecc9230
这不需要对生成的客户端或绑定进行任何修改。