2014-09-01 8 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