XML Transmission examples - Java
public class ExampleServlet extends HttpServlet {
public ExampleServlet() {
super();
}
// init forward URI from properties
public synchronized void init(ServletConfig config)
throws javax.servlet.ServletException {
super.init(config);
}
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
String responseMessage = null;
String error = errorHeader;
String requestMessage = "<?xml version="1.0"?>"+
"<productAvailabilityRequest version="1.1">"+
"<client number="demoxml" password="demoxml"/>"+
"<products>"+
"<product number="0096537" quantity="1"/>"+
"</products>"+
"</productAvailabilityRequest>";
// ssl support
System.setProperty("java.protocol.handler.pkgs",
"com.sun.net.ssl.internal.www.protocol");
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
// keystore for certificates,
// certificate from https://test.e-galexis.com/
// has to be imported into keystore
// (e.g. keytool -import -keystore cacerts -alias galexis -file galexis.cer)
System.setProperty("javax.net.ssl.trustStore",
"cacerts");
// keystore password
System.setProperty("javax.net.ssl.trustStorePassword",
"changeit");
URL serverURL = new URL("https://test.e-galexis.com/testPOS/");
try {
// send POST message
HttpURLConnection httpConnection =
(HttpURLConnection) serverURL.openConnection();
// Prepare method
httpConnection.setRequestMethod("POST");
// follow possible redirects
httpConnection.setFollowRedirects(true);
// Prepare for both input and output
httpConnection.setDoInput(true);
httpConnection.setDoOutput(true);
// Turn off caching
httpConnection.setUseCaches(false);
// content type
httpConnection.setRequestProperty("Content-Type", "text/xml");
// Write post data
DataOutputStream out = new DataOutputStream(
httpConnection.getOutputStream());
out.writeBytes(requestMessage);
out.flush();
out.close();
BufferedReader aiResult = new BufferedReader(
new InputStreamReader(httpConnection.getInputStream()));
String line;
StringBuffer responseMessage = new StringBuffer();
while ((line = aiResult.readLine()) != null) {
responseMessage.append(line);
}
System.out.println(responseMessage);
} catch (Exception e) {
//send the error message to client
}
}
}