我正在使用.NET WCF客户端和C#来使用Java WebService。在向此Web服务发送请求时,封装的WCF客户端始终使用元素mustunderstand=1
,即使使用定制的MessageInspector实现IClientMessageInspector
更改方法IClientMessageInspector.BeforeSendRequest
中的标题元素。我猜BasicHttpBinding与BasicHttpSecurityMode.TrasportWithCredentials,总是把这个元素放在请求的头部,忽略这些手动添加的头文件。如何使用BasicHttpBinding和BasicHttpSecurityMode.TransportWithCredential将soap头部元素“mustunderstand = 0”设置为soap请求:
这个mustunderstand=1
头元素在服务器端触发错误,这个服务器不在我们的控制之下。 Id喜欢避免这个标题元素响应错误将mustunderstand=1
的值更改为mustunderstand=0
。
所以有一个技巧或技巧来改变这个mustunderstand
头参数的值?
要ilustrate查看由FIDDLER和MessageInspector跟踪的REQUEST RAW,RESPONSE RAW。
REQUEST RAW:
POST "WSDLADDRESS" HTTP/1.1
MIME-Version: 1.0
Content-Type: multipart/related; type="application/xop+xml";start="<http://tempuri.org/0>";boundary="uuid:a0f5ee5b-4543-4aa9-85be-f7af4e8ea289+id=1";start-info="text/xml"
SOAPAction: "NAMEOFSOAPACTION"
Host: wwwh.cnj.jus.br
Content-Length: 5013
Expect: 100-continue
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
--uuid:a0f5ee5b-4543-4aa9-85be-f7af4e8ea289+id=1
Content-ID: <http://tempuri.org/0>
Content-Transfer-Encoding: 8bit
Content-Type: application/xop+xml;charset=utf-8;type="text/xml"
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"><s:Header><o:Security s:mustUnderstand="1" xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"><u:Timestamp u:Id="_0"><u:Created>2014-09-01T14:06:59.225Z</u:Created><u:Expires>2014-09-01T14:11:59.225Z</u:Expires></u:Timestamp><o:BinarySecurityToken u:Id="uuid-562fb884-7bc7-4323-ac5e-29556b6c85aa-1" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3" EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary"><xop:Include href="cid:http://tempuri.org/1/635451664301180633" xmlns:xop="http://www.w3.org/2004/08/xop/include"/></o:BinarySecurityToken><Signature xmlns="http://www.w3.org/2000/09/xmldsig#"><SignedInfo><CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/><SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/><Reference URI="#_0"><Transforms><Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/></Transforms><DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/><DigestValue>cIusnusCxdoCXm4BhihEXGC3MMw=</DigestValue></Reference></SignedInfo><SignatureValue>LC4d/YhYHKRocSmORNWC0oQzCrLkG00b1iSZxDL/TflCQ3pJa7qYxPY07oUR4Ngull22V9fn1QBl1NOoRii8JjK1XjJYW8XQ+pVVMnf6l2guYJjYmTp7y2p+UT3JAQrlJIYj2sAu38obsthemAtec/miY3K5anq/AYTIr1slkyDJsm23w4boq3RfTD9mbU+U+KSF71vFi5RK8ryX/i9FEsayp0N0Hpw5SP/lOEI2IkOmw4Mpg2slCLIcQWJMuP+g6aZgLviVTQBgUE3ETwdVQIogmHChfeAtHCD9Knid4Bndyldq0WoTWzGJZ9PL8PXZJIMzQtYLP+XG/emAb3Bp1g==</SignatureValue><KeyInfo><o:SecurityTokenReference><o:Reference ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3" URI="#uuid-562fb884-7bc7-4323-ac5e-29556b6c85aa-1"/></o:SecurityTokenReference></KeyInfo></Signature></o:Security></s:Header><s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><consultarAvisosPendentes xmlns="http://www.cnj.jus.br/servico-intercomunicacao-2.2/"><idRepresentado xmlns="http://www.cnj.jus.br/tipos-servico-intercomunicacao-2.2"/><idConsultante xmlns="http://www.cnj.jus.br/tipos-servico-intercomunicacao-2.2"/><senhaConsultante xmlns="http://www.cnj.jus.br/tipos-servico-intercomunicacao-2.2"/><dataReferencia xmlns="http://www.cnj.jus.br/tipos-servico-intercomunicacao-2.2">01/09/2014</dataReferencia></consultarAvisosPendentes></s:Body></s:Envelope>
--uuid:a0f5ee5b-4543-4aa9-85be-f7af4e8ea289+id=1
Content-ID: <http://tempuri.org/1/635451664301180633>
Content-Transfer-Encoding: binary
Content-Type: application/octet-stream
0��0�ΠZN�(K��20� ���j0
*�H��
�0o1
0 UBR10U
(...certificate data...)
�����2o�w�|� ������#�d�e�Z9�KI��l����ɵ�v���Ka�����q�晚=]�ht���l�T�x�7<IS�[email protected]��T�d�30��T;l��3�ЉJ�K(���tP��oU�結$�"�4�4�x����p����\ނl�/0鈾yN�
iu����y�-��Z�J���<���P�5�i}��;&u�t�DH�oc��@D�Mf}ue��I\Mk�鐈�^gF�_T'䌁U��ёN����qy;�P_����7F�dz��qLL
�`g>
�C�﨨�%��a;���z�����n�$��O�Vz4��ѡ�D��V�3�X�!m��y���E��E��};��)�Jlv��+��6���RK
�����w��k���@�XIB����?��qC��ġ��6n{n��s�̔-���
|�����@+�Jv�r�%̓p�� *gy4��(�hA�⮫*m��bU��c�>����}��h�����Y�(
--uuid:a0f5ee5b-4543-4aa9-85be-f7af4e8ea289+id=1--
响应RAW:
HTTP/1.1 500 Internal Server Error
Date: Mon, 01 Sep 2014 14:07:10 GMT
X-Powered-By: Servlet 2.5; JBoss-5.0/JBossWeb-2.1
Content-Type: text/xml;charset=UTF-8
Vary: Accept-Encoding
Connection: close
Content-Length: 480
<env:Envelope xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'><env:Header></env:Header><env:Body><env:Fault xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'><faultcode xmlns:codeNS='http://schemas.xmlsoap.org/soap/envelope/'>codeNS:MustUnderstand</faultcode><faultstring>Unprocessed 'mustUnderstand' header element: {http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd}Security</faultstring></env:Fault></env:Body></env:Envelope>
MessageInspector:
class MessageInspector : IClientMessageInspector
{
object IClientMessageInspector.BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
{
request.Headers.Add(MessageHeader.CreateHeader
(request.Headers[0].Name,
request.Headers[0].Namespace,
string.Empty,
false,
request.Headers[0].Actor,
request.Headers[0].Relay)
);
request.Headers.RemoveAt(0);
return Convert.DBNull;
}
public void AfterReceiveReply(ref Message reply, object correlationState)
{
}
}
请求头前:
请求头后:
你可以显示你的'MessageInspector'实现吗? – barrick 2014-09-01 15:15:57
Hy @barrick,好的!我在帖子中添加了它。但即使手动添加标题,由wcf创建的客户端也会忽略“false”参数,并将“mustunderstand = 1”元素放在标题中。 – Castello 2014-09-01 16:41:14
我明白了。如果你在'request.Headers.RemoveAt(0)'方法调用之后放置了一个断点,那么你的消息是否具有所需的标题? – barrick 2014-09-01 17:49:38