none
Integração Java e .NET 1.1 RRS feed

  • Pergunta

  • Pessoal,

    Estou com um problema meio macabro de resolver, bom preciso acessar um serviço construído em Java com autenticação, o problema é para enviar o SoapHeader da seguinte forma:

      <soapenv:Header>
        <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
          <wsse:UsernameToken>
            <wsse:Username>USERNAME</wsse:Username>
            <wsse:Password>PASSWORD</wsse:Password>
          </wsse:UsernameToken>
        </wsse:Security>
      </soapenv:Header>
    

    Sendo que utilizando o WSE 2.0 eu consegui enviar o XML desta forma:

    <soapenv:Header>
    	<wsse:Security d1p1:mustUnderstand=\"1\" xmlns:d1p1=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\">
    		<wsu:Timestamp wsu:Id=\"Timestamp-d1cbdfed-7d51-41d4-839e-3acdc7f1872c\" xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\">
    			<wsu:Created>2010-10-22T18:02:32Z</wsu:Created>
    			<wsu:Expires>2010-10-22T18:07:32Z</wsu:Expires>
    		</wsu:Timestamp>
    		<wsse:UsernameToken xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\" wsu:Id=\"SecurityToken-64f2f515-03fd-47d2-82fb-6c7ec44c84c1\">
    			<wsse:Username>USERNAME</wsse:Username>
    			<wsse:Password Type=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText\">PASSWORD</wsse:Password>
    			<wsse:Nonce>+UETSVrAVQmp3iQEUNa7fQ==</wsse:Nonce>
    			<wsu:Created>2010-10-22T18:02:32Z</wsu:Created>
    		</wsse:UsernameToken>
    	</wsse:Security>
    </soapenv:Header>
    

    Mesmo assim me retorna um erro: The message expired.

    Se alguém tiver alguma dica de como fazer essa autenticação, o impressionante é que utilizando o SOAPUI ele funciona perfeitamente talvez por que é JAVA também.

    quinta-feira, 4 de novembro de 2010 14:23

Respostas

  • Pessoal consegui resolver este problema implementando SoapExtesion, ta certo que ainda da para melhorar o código, mais desta forma funcionou bem.

    Obrigado a todos que de alguma forma tentaram contribuir.

    	public class SoapExtensionGeneric : System.Web.Services.Protocols.SoapExtension
    	{
    		public bool outgoing = true;
    		public bool incoming = false;
    		private Stream outputStream;
    		public Stream oldStream;
    		public Stream newStream;
    		public string filename;
    
    		public override Stream ChainStream(Stream stream)
    		{
    			this.outputStream = stream;
    			oldStream = stream;
    			newStream = new MemoryStream();
    			return newStream;
    		}
    
    
    		public override object GetInitializer(LogicalMethodInfo methodInfo, SoapExtensionAttribute attribute) 
    		{
    			return null;
    		}
    
    	
    		public override object GetInitializer(Type WebServiceType) 
    		{
    			return null;		  }
    
    	
    		public override void Initialize(object initializer) 
    		{
    			filename = (string) initializer;
    		}
    
    
    		public override void ProcessMessage(SoapMessage message)
    		{
    			StreamReader readStr;
    			StreamWriter writeStr;
    			string soapMsg1;
    			XmlDocument xDoc = new XmlDocument();
    			switch (message.Stage)
    			{
    				case SoapMessageStage.BeforeSerialize:
    					break;
    				case SoapMessageStage.AfterSerialize:
    				{	
    										String soapBodyString = getXMLFromCache().Replace("soap", "soapenv");
    										String BodString = "<soapenv:Body>";
    					int pos1 = soapBodyString.IndexOf(BodString) + BodString.Length;
    					int pos2 = soapBodyString.Length - pos1;
    					soapBodyString = soapBodyString.Substring(pos1, pos2);
    					
    					string invokeMethod = soapBodyString.Substring(1, soapBodyString.IndexOf(">")-1);
    					string repalceInvokeMethod = invokeMethod.Substring(0, invokeMethod.IndexOf(" "));
    
    					if(invokeMethod.IndexOf("http://ncl.service") > -1)
    						soapBodyString.Replace(invokeMethod, "ncl" + repalceInvokeMethod);
    					else if(invokeMethod.IndexOf("http://ope.service") > -1)
    						soapBodyString.Replace(invokeMethod, "ope" + repalceInvokeMethod);
    					else if(invokeMethod.IndexOf("http://ade.service") > -1)
    						soapBodyString.Replace(invokeMethod, "ade" + repalceInvokeMethod);
    					else if(invokeMethod.IndexOf("http://sia.service") > -1)
    						soapBodyString.Replace(invokeMethod, "sia" + repalceInvokeMethod);
    					else if(invokeMethod.IndexOf("http://leg.service") > -1)
    						soapBodyString.Replace(invokeMethod, "leg" + repalceInvokeMethod);
    					else if(invokeMethod.IndexOf("http://service.icatu") > -1)
    						soapBodyString.Replace(invokeMethod, "ser" + repalceInvokeMethod);
    
    
    					soapBodyString = "<soapenv:Body>" + soapBodyString;
    										String soapEnvelopeBeginString = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ope=\"http://ope.service.kiprev.kiman.com.br/\">";
    					
    					String soapEnvHeaderString = "<soapenv:Header>";					
    					soapEnvHeaderString +=			"<wsse:Security xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\">";
    					soapEnvHeaderString +=				"<wsse:UsernameToken>";
    					soapEnvHeaderString +=					"<wsse:Username>usuário</wsse:Username>";
    					soapEnvHeaderString +=					"<wsse:Password>senha</wsse:Password>";
    					soapEnvHeaderString +=				"</wsse:UsernameToken>";
    					soapEnvHeaderString +=			"</wsse:Security>";
    					soapEnvHeaderString +=		"</soapenv:Header>";
    
    					Stream appOutputStream = new MemoryStream();
    					StreamWriter soapMessageWriter = new StreamWriter(appOutputStream);
    									soapMessageWriter.Write(soapEnvelopeBeginString);
    
    					soapMessageWriter.Write(soapEnvHeaderString);
    					soapMessageWriter.Flush();
    					appOutputStream.Flush();
    					appOutputStream.Position = 0;
    					StreamReader reader = new StreamReader(appOutputStream);
    					StreamWriter writer = new StreamWriter(this.outputStream);
    					writer.Write(reader.ReadToEnd());
    					writer.Flush();
    					appOutputStream.Close();
    					this.outgoing = false;
    					this.incoming = true;
    					break;
    				}
    				case SoapMessageStage.BeforeDeserialize:
    				{
    					readStr = new StreamReader(oldStream);
    					writeStr = new StreamWriter(newStream);
    					soapMsg1 = readStr.ReadToEnd();
    					xDoc.LoadXml(soapMsg1);
    					soapMsg1 = xDoc.InnerXml;
    					writeStr.Write(soapMsg1);
    					writeStr.Flush();
    					newStream.Position = 0;
    					break;
    				}
    				case SoapMessageStage.AfterDeserialize:
    					break;
    				default:
    					throw new Exception("invalid stage!");
    			}
    		}
    	
    
    		public string getXMLFromCache()
    		{
    			newStream.Position = 0; 
    			string strSOAPresponse = ExtractFromStream(newStream);
    			return strSOAPresponse;
    		}
    
    
    		private String ExtractFromStream(Stream target)
    		{
    			if (target != null)
    				return (new StreamReader(target)).ReadToEnd();
    			return "";
    		}
    	}
    
    

    Links de apoio: http://msdn.microsoft.com/pt-br/library/system.web.services.protocols.soapextension.aspx http://www.codeproject.com/KB/cpp/SOAP_Header_Extension.aspx

    Se conseguir ajudar alguém está ai!!!!

    Abraços.

    quarta-feira, 10 de novembro de 2010 15:55

Todas as Respostas

  • Pessoal consegui resolver este problema implementando SoapExtesion, ta certo que ainda da para melhorar o código, mais desta forma funcionou bem.

    Obrigado a todos que de alguma forma tentaram contribuir.

    	public class SoapExtensionGeneric : System.Web.Services.Protocols.SoapExtension
    	{
    		public bool outgoing = true;
    		public bool incoming = false;
    		private Stream outputStream;
    		public Stream oldStream;
    		public Stream newStream;
    		public string filename;
    
    		public override Stream ChainStream(Stream stream)
    		{
    			this.outputStream = stream;
    			oldStream = stream;
    			newStream = new MemoryStream();
    			return newStream;
    		}
    
    
    		public override object GetInitializer(LogicalMethodInfo methodInfo, SoapExtensionAttribute attribute) 
    		{
    			return null;
    		}
    
    	
    		public override object GetInitializer(Type WebServiceType) 
    		{
    			return null;		  }
    
    	
    		public override void Initialize(object initializer) 
    		{
    			filename = (string) initializer;
    		}
    
    
    		public override void ProcessMessage(SoapMessage message)
    		{
    			StreamReader readStr;
    			StreamWriter writeStr;
    			string soapMsg1;
    			XmlDocument xDoc = new XmlDocument();
    			switch (message.Stage)
    			{
    				case SoapMessageStage.BeforeSerialize:
    					break;
    				case SoapMessageStage.AfterSerialize:
    				{	
    										String soapBodyString = getXMLFromCache().Replace("soap", "soapenv");
    										String BodString = "<soapenv:Body>";
    					int pos1 = soapBodyString.IndexOf(BodString) + BodString.Length;
    					int pos2 = soapBodyString.Length - pos1;
    					soapBodyString = soapBodyString.Substring(pos1, pos2);
    					
    					string invokeMethod = soapBodyString.Substring(1, soapBodyString.IndexOf(">")-1);
    					string repalceInvokeMethod = invokeMethod.Substring(0, invokeMethod.IndexOf(" "));
    
    					if(invokeMethod.IndexOf("http://ncl.service") > -1)
    						soapBodyString.Replace(invokeMethod, "ncl" + repalceInvokeMethod);
    					else if(invokeMethod.IndexOf("http://ope.service") > -1)
    						soapBodyString.Replace(invokeMethod, "ope" + repalceInvokeMethod);
    					else if(invokeMethod.IndexOf("http://ade.service") > -1)
    						soapBodyString.Replace(invokeMethod, "ade" + repalceInvokeMethod);
    					else if(invokeMethod.IndexOf("http://sia.service") > -1)
    						soapBodyString.Replace(invokeMethod, "sia" + repalceInvokeMethod);
    					else if(invokeMethod.IndexOf("http://leg.service") > -1)
    						soapBodyString.Replace(invokeMethod, "leg" + repalceInvokeMethod);
    					else if(invokeMethod.IndexOf("http://service.icatu") > -1)
    						soapBodyString.Replace(invokeMethod, "ser" + repalceInvokeMethod);
    
    
    					soapBodyString = "<soapenv:Body>" + soapBodyString;
    										String soapEnvelopeBeginString = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ope=\"http://ope.service.kiprev.kiman.com.br/\">";
    					
    					String soapEnvHeaderString = "<soapenv:Header>";					
    					soapEnvHeaderString +=			"<wsse:Security xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\">";
    					soapEnvHeaderString +=				"<wsse:UsernameToken>";
    					soapEnvHeaderString +=					"<wsse:Username>usuário</wsse:Username>";
    					soapEnvHeaderString +=					"<wsse:Password>senha</wsse:Password>";
    					soapEnvHeaderString +=				"</wsse:UsernameToken>";
    					soapEnvHeaderString +=			"</wsse:Security>";
    					soapEnvHeaderString +=		"</soapenv:Header>";
    
    					Stream appOutputStream = new MemoryStream();
    					StreamWriter soapMessageWriter = new StreamWriter(appOutputStream);
    									soapMessageWriter.Write(soapEnvelopeBeginString);
    
    					soapMessageWriter.Write(soapEnvHeaderString);
    					soapMessageWriter.Flush();
    					appOutputStream.Flush();
    					appOutputStream.Position = 0;
    					StreamReader reader = new StreamReader(appOutputStream);
    					StreamWriter writer = new StreamWriter(this.outputStream);
    					writer.Write(reader.ReadToEnd());
    					writer.Flush();
    					appOutputStream.Close();
    					this.outgoing = false;
    					this.incoming = true;
    					break;
    				}
    				case SoapMessageStage.BeforeDeserialize:
    				{
    					readStr = new StreamReader(oldStream);
    					writeStr = new StreamWriter(newStream);
    					soapMsg1 = readStr.ReadToEnd();
    					xDoc.LoadXml(soapMsg1);
    					soapMsg1 = xDoc.InnerXml;
    					writeStr.Write(soapMsg1);
    					writeStr.Flush();
    					newStream.Position = 0;
    					break;
    				}
    				case SoapMessageStage.AfterDeserialize:
    					break;
    				default:
    					throw new Exception("invalid stage!");
    			}
    		}
    	
    
    		public string getXMLFromCache()
    		{
    			newStream.Position = 0; 
    			string strSOAPresponse = ExtractFromStream(newStream);
    			return strSOAPresponse;
    		}
    
    
    		private String ExtractFromStream(Stream target)
    		{
    			if (target != null)
    				return (new StreamReader(target)).ReadToEnd();
    			return "";
    		}
    	}
    
    

    Links de apoio: http://msdn.microsoft.com/pt-br/library/system.web.services.protocols.soapextension.aspx http://www.codeproject.com/KB/cpp/SOAP_Header_Extension.aspx

    Se conseguir ajudar alguém está ai!!!!

    Abraços.

    quarta-feira, 10 de novembro de 2010 15:55
  • Olá Tiago,

    Estou com o mesmo problema.

    Mas com sou um novato nestas coisas de Webservices, não consegui apanhar a tua dica.

    Podes deixar um exemplo de como utilizar a tua classe dentro de um cliente de Webservice?

     

    Obrigado

    Zé Teixeira

     

     

    sábado, 11 de junho de 2011 11:09