import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import com.sun.org.apache.xerces.internal.impl.dv.util.Base64; /* * Java - Beispiel zur Ansteuerung des XML-Dienstes der egeko-Auftrags-Schnittstelle (kurz: 'v2-order') * */ public final class XmlAdapter { private final String userAuth; /** * URL zum Dienst */ private static final String SERVICE_ENDPOINT = "https://ws.optadata.com/egeko-service-order-v2/xmlservice" public XmlAdapter(String userName, String password) { userAuth = new String(Base64.encode((userName + ":" + password).getBytes())).trim(); } public final String sendXml(String requestString) throws IOException { HttpURLConnection connection = (HttpURLConnection) new URL(SERVICE_ENDPOINT).openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Authorization", "BASIC " + userAuth.trim()); connection.setDoOutput(true); connection.setDoInput(true); connection.connect(); OutputStream out = connection.getOutputStream(); out.write(requestString.getBytes()); out.flush(); int response = connection.getResponseCode(); try { if (response == HttpURLConnection.HTTP_OK) { InputStream in = connection.getInputStream(); ByteArrayOutputStream stringWriter = new ByteArrayOutputStream(); int b; while ((b = in.read()) != -1) { stringWriter.write((char) b); } String responseText = new String(stringWriter.toByteArray()); responseText = responseText.trim(); return responseText; } else { InputStream in = connection.getErrorStream(); ByteArrayOutputStream stringWriter = new ByteArrayOutputStream(); int b; while ((b = in.read()) != -1) { stringWriter.write((char) b); } String responseText = new String(stringWriter.toByteArray()); return responseText; } } finally { connection.disconnect(); } } }