Witam,
Próbuję napisać programik, który będzie wysyłał request SOAP-a ( z pliku request.xml ) i zwracał response ( do pliku response.xml ) .
Póki co udało mi się znaleźć
http://www-128.ibm.com/developerworks/xml/library/x-jaxmsoap/
i troche pozmieniać i wyszłedł mi działający program, którego cały kod umieszcze na końcu posta.
Wszystko fajnie działa gdy Web Service jest udostępniony przez HTTP , ale
już się krzaczy gdy chcę wysłać ( i odebrać ) SOAP-a przez HTTPS ... z tego
co zdążyłem wyczytać to nie do końca taka prosta sprawa sprawić żeby działał taki program przez HTTPS ..
gugluje dalej, kupiłem nawet książke :) , ale boje się że nie uda mi się tego przejść na czas ...
Stąd moja prośba o pomoc, może ktoś miał podobny problem ..
Niestety mam dostępu do żadnego zewnętrznego serwera z https, żeby umieścić przykładową usługę,
testuje w lokalnej sieci z Apachem z SSL .
Dzięki z góry
Oto kod programu : ( używam jre i jdk 1.6 )
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.net.URL;
import java.security.Security;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
public class BringSOAP {
public static void main(String args[]) {
if (args.length == 0)
{
System.out.println("Usage : BringSOAP <WebService - URL> ") ;
}
else
try {
System.setProperty("java.protocol.handler.pkgs",
"com.sun.net.ssl.internal.www.protocol");
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
// System.setProperty("javax.net.debug", "all" );
//First create the connection
SOAPConnectionFactory soapConnFactory =
SOAPConnectionFactory.newInstance();
SOAPConnection connection =
soapConnFactory.createConnection();
//Next, create the actual message
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage message = messageFactory.createMessage();
// Create objects for the message parts
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPBody body = envelope.getBody();
//Populate the Message
StreamSource preppedMsgSrc = new StreamSource(
new FileInputStream("request.xml"));
soapPart.setContent(preppedMsgSrc);
//Save the message
message.saveChanges();
// Check the input
System.out.println("\nREQUEST:\n");
message.writeTo(System.out);
System.out.println();
//Send the message and get a reply
//Set the destination
URL destination = new URL(args[0]);
// Send the message
SOAPMessage reply = connection.call(message, destination);
//Check the output
//Create the transformer
TransformerFactory transformerFactory =
TransformerFactory.newInstance();
Transformer transformer =
transformerFactory.newTransformer();
//Extract the content of the reply
Source sourceContent = reply.getSOAPPart().getContent();
//Set the output for the transformation
FileOutputStream out; // declare a file output object
PrintStream p; // declare a print stream object
// Create a new file output stream
out = new FileOutputStream("response.xml");
// Connect print stream to the output stream
p = new PrintStream( out );
StreamResult result = new StreamResult(p);
transformer.transform(sourceContent, result);
p.println();
p.close();
//Close the connection
connection.close();
}
catch(Exception e)
{
FileOutputStream out; // declare a file output object
PrintStream p; // declare a print stream object
try
{
System.out.println(e.getMessage());
out = new FileOutputStream("error.txt");
// Connect print stream to the output stream
p = new PrintStream( out );
p.println(e.getMessage());
p.close();
}
catch(Exception f)
{
System.out.println(f.getMessage());
}
}
}
}