Java by API/javax.xml.soap/MessageFactory

Материал из Java эксперт
Перейти к: навигация, поиск

MessageFactory: createMessage()

   <source lang="java">
 

import javax.xml.soap.MessageFactory; import javax.xml.soap.Name; import javax.xml.soap.SOAPBody; import javax.xml.soap.SOAPBodyElement; import javax.xml.soap.SOAPEnvelope; import javax.xml.soap.SOAPHeader; import javax.xml.soap.SOAPHeaderElement; import javax.xml.soap.SOAPMessage; import javax.xml.soap.SOAPPart; import javax.xml.transform.Source; public class Main {

 public static void main(String[] args) throws Exception {
   SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();
   SOAPPart soapPart = soapMessage.getSOAPPart();
   SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
   SOAPHeader soapHeader = soapEnvelope.getHeader();
   SOAPHeaderElement headerElement = soapHeader.addHeaderElement(soapEnvelope.createName(
       "Signature", "SOAP-SEC", "http://schemas.xmlsoap.org/soap/security/2000-12"));
   SOAPBody soapBody = soapEnvelope.getBody();
   soapBody.addAttribute(soapEnvelope.createName("id", "SOAP-SEC",
       "http://schemas.xmlsoap.org/soap/security/2000-12"), "Body");
   Name bodyName = soapEnvelope.createName("FooBar", "z", "http://example.ru");
   SOAPBodyElement gltp = soapBody.addBodyElement(bodyName);
   Source source = soapPart.getContent();
 }

}


 </source>
   
  
 
  



MessageFactory.createMessage(MimeHeaders headers, InputStream in)

   <source lang="java">
 

import java.io.FileInputStream; import java.net.URL; import javax.xml.namespace.QName; import javax.xml.soap.MessageFactory; import javax.xml.soap.SOAPMessage; import javax.xml.ws.Dispatch; import javax.xml.ws.Service; public class Main {

 public static void main(String args[]) throws Exception {
   URL url = new URL("http://api.google.ru/GoogleSearch.wsdl");
   QName serviceName = new QName("urn:GoogleSearch", "GoogleSearchService");
   QName portName = new QName("urn:GoogleSearch", "GoogleSearchPort");
   Service service = Service.create(url, serviceName);
   Dispatch<SOAPMessage> dispatch = service.createDispatch(portName, SOAPMessage.class,
       Service.Mode.MESSAGE);
   SOAPMessage request = MessageFactory.newInstance().createMessage(null,
       new FileInputStream("yourGoogleKey.xml"));
   SOAPMessage response = dispatch.invoke(request);
   response.writeTo(System.out);
 }

}


 </source>