How to send input request in XML API?
I am writing XMLAPI and passing request which is in XML to get the response in XML .
Here is the input request I am passing
<ASBMessage version="2.0" id="[GUID]"> <Header> <Endpoint> <ID value="ServiceMonitor"/> </Endpoint> <Context id="request"/> <Itinerary> <Endpoint uri="asb://asb.twcable.com/dsb/channel/common"/> </Itinerary> </Header> <Body> <Account operation="query"> <ID value="12345678901" ns="UCAN"/> </Account> </Body> </ASBMessage>
Here is the connection I am making to XMLAPI.
@Component public class DSBClient { private static Logger log = LoggerFactory.getLogger("DSBClient.class"); String cookie = ""; public String getUCanClient(String ucanRequestStr) { log.info("Entering getUCanClient ..." + ucanRequestStr); String msg = ""; byte[] bytes = null; try { URL url = new URL(ApplicationPropertyReader.dsbUrl); bytes = ucanRequestStr.getBytes(); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setAllowUserInteraction(false); connection.setDoInput(true); connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "text/xml"); connection.setRequestProperty("Content-Length", String.valueOf(bytes.length)); connection.setRequestProperty("Cookie", cookie); connection.connect(); java.io.OutputStream out = connection.getOutputStream(); out.write(bytes); msg = new String(bytes, StandardCharsets.UTF_8); log.info("output 3 = " + msg); out.close(); int code = connection.getResponseCode(); // msg = connection.getResponseMessage(); if (code == HttpURLConnection.HTTP_OK) { // Cookie Info might be needed , than add it - for the time being keeping blank log.info("Response OK"); } else { throw new DSBException("HTTP Resonse code not OK : " + code); } connection.disconnect(); } catch (ProtocolException e) { throw new DSBException("ProtocolException : " + e.getMessage(), e); } catch (MalformedURLException e) { throw new DSBException("MalformedURLException : " + e.getMessage(), e); } catch (IOException e) { throw new DSBException("IOException : " + e.getMessage(), e); } return msg; } }
I am getting response code "OK" but unable to get the expected response. I believe my request needs to be sent along with connection.
I am expecting to get
<ASBMessage timestamp="2018-10-10T13:06:48.723" version="2.0" id="b874a232d547450c8daf14ccf54e79d8"> <Header> <Endpoint> <ID value="DSB"/> </Endpoint> <Context id="Response"/> <Status id="OK"/> <PropertyList id="asb"> <Property id="thread" value="8C86C9E6-A2E8-2D08-A413-A85BA14FB02C"/> </PropertyList> </Header> <Body items="1"> <Account status="ok" operation="QUERY"> <ID ns="DSB" value="624423CB-6CB3-E99D-8F1B-227289462562"/> <ID ns="UCAN" value="12345678901"/> <Status id="Active"/> <Audit creator="{Endpoint-Identifier}" timeCreated="181010130634518Z"/> </Account> </Body> </ASBMessage>