2014-09-01 33 views
0

我正在使用.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 &apos;mustUnderstand&apos; 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) 
     { 
     } 
    } 

请求头前:

Request Headers Before

请求头后:

Request Headers After

+0

你可以显示你的'MessageInspector'实现吗? – barrick 2014-09-01 15:15:57

+0

Hy @barrick,好的!我在帖子中添加了它。但即使手动添加标题,由wcf创建的客户端也会忽略“false”参数,并将“mustunderstand = 1”元素放在标题中。 – Castello 2014-09-01 16:41:14

+0

我明白了。如果你在'request.Headers.RemoveAt(0)'方法调用之后放置了一个断点,那么你的消息是否具有所需的标题? – barrick 2014-09-01 17:49:38

回答

1

好了...这将需要一些工作,得到正确的。我不知道您是否熟悉绑定中的通道堆栈,但基本上,消息将通过WCF中的various operations流动,每个消息都将以某种方式转换消息。

This answer,并且使用自定义消息编码器是您需要做的事情,因为WCF在修改标头以删除标头后正在应用“正确”标头(因为它看到了它,由于指定的绑定) mustUnderstand属性。

这是一个相当复杂的领域,我不得不使用它来获取由WCF生成的SOAP头,以符合WSE 3.0用于创建的东西,并在Security头中包含一个二进制令牌;但是有一些试验和错误,这是一个非常强大的功能。

+0

Hy @barrick,我会尽快解答您的答案,并在我收到结果后尽快回复! :)我真的很喜欢你的答案,它似乎工作! TKS! – Castello 2014-09-01 21:35:11