JAVA

JAVA/URL/URLConnection/XML/ASMX/.NET

IT History 2022. 7. 13. 09:59
728x90
반응형

 

SOAP통신 후 결과값 받아보기

 

 

private final FastStringBuffer oBuffer = new FastStringBuffer();

private String urlConnectionTest() throws Exception {
        String sEndPoint = "접속할 URL";
        
        //Http 연결
        URL url = new URL(sEndPoint);
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.addRequestProperty("Content-Type", "text/xml; charset=utf-8");
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
		
        // soap Xml데이터 전송문 작성
        oBuffer.append("<?xml version="1.0"  encoding="utf-8"?>");
        oBuffer.append("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">");
        oBuffer.append("<soap:Body>");
        
        /*................생략..................*/
        
        oBuffer.append("</soap:Body>");
        oBuffer.append("</soap:Envelope>");
        
        //OutputStream에 쓰기
        String xmlString = new String(oBuffer.toString());
        wr.write(xmlString);
		
        //flush : Stream에 남아 있는 데이터를 강제로 내보내는 역할
        wr.flush();
		
        // 리턴된 결과 읽기
        String inputLine;
        String returnStr = "";
        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
        
        while ((inputLine = in.readLine()) != null)
        {
        	returnStr += inputLine;
        }

        //XML 파싱
        StringReader reader = new StringReader(returnStr);
        InputSource is = new InputSource(reader);
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = builder.parse(is);

        if (doc == null)
        {
        	throw new Exception("ERROR :: document is null");
        }
        return returnStr;
}
728x90
반응형