http post 방식으로 header를 설정한 후 xml 데이터를 서버에 전달 후
그 결과를 받는다.
첨부된 파일을 압축을 풀면 lib 디렉토리 안의 jar 파일들을
%CATALINA_HOME%\common\lib 에 위치시키거나
CALSSPASS를 잡아준 후 이용하면 된다.
<%@ page import="org.apache.commons.httpclient.HttpClient" %>
<%@ page import="org.apache.commons.httpclient.methods.PostMethod" %>
<%@ page import ="java.net.*" %>
<%@ page import ="java.io.*" %>
<%
String orgMsg="abcdefg";
String encMsg = null;
String header1 = "header1";
String header2 = "header2";
String header3 = "header3";
String ContentsType="text/xml";
AES256 aes = new AES256();
encMsg = aes.encrypt(msg);
String xmldata = "<? xml version=\"1.0\" encoding=\"euc-kr\"?> "
+"<! DOCTYPE lgtpims:schema>"
+"<schema targetns=\"http://www.aaa.com\""
+"version=\"1.0\" xmlns=\"http://www.w3.org/2000/10/XMLSchema\">"
+"<parameter method=\"add\">"
+"<element-value name=\"addelement\">aaaaa</element-value>"
+"<element-value name=\"addelementtype\">bbbbb</element-value>"
+"</parameter>"
+"</schema>" ;
String strURL = "http://www.aaa.com/aaa";
PostMethod post = new PostMethod(strURL);
post.setRequestHeader("Content-type", "text/xml; charset=ISO-8859-1");
post.setRequestHeader("header1","header1");
post.setRequestHeader("header2",header2);
post.setRequestHeader("header3",header3");
post.setRequestHeader("Content-Length",Integer.toString(xmldata.length()));
post.setRequestBody(xmldata);
HttpClient httpclient = new HttpClient();
try {
int result = httpclient.executeMethod(post);
// Display status code
out.println("<br>Response status code: " + result);
// Display response
out.println("<br>Response header: "+post.getResponseHeader("Status-Line"));
out.println("<br>Response body: ");
out.println(post.getResponseBodyAsString());
} finally {
// Release current connection to the connection pool once you are done
post.releaseConnection();
}
%>
2010년 3월 29일 월요일
JAVA AES 256 암호화
aes256 암호화
import sun.misc.BASE64Encoder;
import sun.misc.BASE64Decoder;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.security.NoSuchAlgorithmException;
public class AES256 {
public static SecretKeySpec getKeySpec() throws IOException, NoSuchAlgorithmException {
byte[] bytes = new byte[32];
SecretKey key = null;
SecretKeySpec spec = null;
/*
File f = new File("aes_key");
if (f.exists()) {
new FileInputStream(f).read(bytes);
} else {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(256);
key = kgen.generateKey();
bytes1 = key.getEncoded();
new FileOutputStream(f).write(bytes1);
}
*/
String keyStr="암호화된 대칭키";
bytes = Base64Utils.base64Decode(keyStr);
spec = new SecretKeySpec(bytes,"AES");
return spec;
}
public String encrypt(String text) throws Exception {
SecretKeySpec spec = getKeySpec();
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, spec);
BASE64Encoder enc = new BASE64Encoder();
return enc.encode(cipher.doFinal(text.getBytes())).toString();
}
public String decrypt(String text) throws Exception {
SecretKeySpec spec = getKeySpec();
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, spec);
BASE64Decoder dec = new BASE64Decoder();
return new String(cipher.doFinal(dec.decodeBuffer(text)));
}
public static void main(String[] args) throws Exception {
String mode = "encrypt";
String text = "!mpluse0218";
String encText = "";
String decText = "";
AES256 aes = new AES256();
encText=aes.encrypt(text);
System.out.println("5ClhQFro0UAI+LrZkilamw==");
System.out.println("enc::"+encText);
decText = aes.decrypt(encText);
System.out.println("dec::"+decText);
}
}
import sun.misc.BASE64Encoder;
import sun.misc.BASE64Decoder;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.security.NoSuchAlgorithmException;
public class AES256 {
public static SecretKeySpec getKeySpec() throws IOException, NoSuchAlgorithmException {
byte[] bytes = new byte[32];
SecretKey key = null;
SecretKeySpec spec = null;
/*
File f = new File("aes_key");
if (f.exists()) {
new FileInputStream(f).read(bytes);
} else {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(256);
key = kgen.generateKey();
bytes1 = key.getEncoded();
new FileOutputStream(f).write(bytes1);
}
*/
String keyStr="암호화된 대칭키";
bytes = Base64Utils.base64Decode(keyStr);
spec = new SecretKeySpec(bytes,"AES");
return spec;
}
public String encrypt(String text) throws Exception {
SecretKeySpec spec = getKeySpec();
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, spec);
BASE64Encoder enc = new BASE64Encoder();
return enc.encode(cipher.doFinal(text.getBytes())).toString();
}
public String decrypt(String text) throws Exception {
SecretKeySpec spec = getKeySpec();
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, spec);
BASE64Decoder dec = new BASE64Decoder();
return new String(cipher.doFinal(dec.decodeBuffer(text)));
}
public static void main(String[] args) throws Exception {
String mode = "encrypt";
String text = "!mpluse0218";
String encText = "";
String decText = "";
AES256 aes = new AES256();
encText=aes.encrypt(text);
System.out.println("5ClhQFro0UAI+LrZkilamw==");
System.out.println("enc::"+encText);
decText = aes.decrypt(encText);
System.out.println("dec::"+decText);
}
}
라벨:
단호박업무,
AES,
aes256,
JAVA,
java aes 256
Base64Utils.java
AES 256암호화시 대칭키를 불러와서 암호화 하는데..암호화 결과가 자꾸 틀렸다..ㅡ_-;;
아놔..고민고민 끝에 암호화된 대칭키를 아래 모듈의 DECODE로 풀어서 넣었더니..되더라..음..
public class Base64Utils {
private static byte[] mBase64EncMap, mBase64DecMap;
/**
* Class initializer. Initializes the Base64 alphabet (specified in RFC-2045).
*/
static {
byte[] base64Map = {
(byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E', (byte)'F',
(byte)'G', (byte)'H', (byte)'I', (byte)'J', (byte)'K', (byte)'L',
(byte)'M', (byte)'N', (byte)'O', (byte)'P', (byte)'Q', (byte)'R',
(byte)'S', (byte)'T', (byte)'U', (byte)'V', (byte)'W', (byte)'X',
(byte)'Y', (byte)'Z',
(byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f',
(byte)'g', (byte)'h', (byte)'i', (byte)'j', (byte)'k', (byte)'l',
(byte)'m', (byte)'n', (byte)'o', (byte)'p', (byte)'q', (byte)'r',
(byte)'s', (byte)'t', (byte)'u', (byte)'v', (byte)'w', (byte)'x',
(byte)'y', (byte)'z',
(byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5',
(byte)'6', (byte)'7', (byte)'8', (byte)'9', (byte)'+', (byte)'/' };
mBase64EncMap = base64Map;
mBase64DecMap = new byte[128];
for (int i=0; i<mBase64EncMap.length; i++)
mBase64DecMap[mBase64EncMap[i]] = (byte) i;
}
/**
* This class isn't meant to be instantiated.
*/
private Base64Utils() {
}
/**
* Encodes the given byte[] using the Base64-encoding,
* as specified in RFC-2045 (Section 6.8).
*
* @param aData the data to be encoded
* @return the Base64-encoded <var>aData</var>
* @exception IllegalArgumentException if NULL or empty array is passed
*/
public static String base64Encode(byte[] aData) {
if ((aData == null) || (aData.length == 0))
throw new IllegalArgumentException("Can not encode NULL or empty byte array.");
byte encodedBuf[] = new byte[((aData.length+2)/3)*4];
// 3-byte to 4-byte conversion
int srcIndex, destIndex;
for (srcIndex=0, destIndex=0; srcIndex < aData.length-2; srcIndex += 3) {
encodedBuf[destIndex++] = mBase64EncMap[(aData[srcIndex] >>> 2) & 077];
encodedBuf[destIndex++] = mBase64EncMap[(aData[srcIndex+1] >>> 4) & 017 |
(aData[srcIndex] << 4) & 077];
encodedBuf[destIndex++] = mBase64EncMap[(aData[srcIndex+2] >>> 6) & 003 |
(aData[srcIndex+1] << 2) & 077];
encodedBuf[destIndex++] = mBase64EncMap[aData[srcIndex+2] & 077];
}
// Convert the last 1 or 2 bytes
if (srcIndex < aData.length) {
encodedBuf[destIndex++] = mBase64EncMap[(aData[srcIndex] >>> 2) & 077];
if (srcIndex < aData.length-1) {
encodedBuf[destIndex++] = mBase64EncMap[(aData[srcIndex+1] >>> 4) & 017 |
(aData[srcIndex] << 4) & 077];
encodedBuf[destIndex++] = mBase64EncMap[(aData[srcIndex+1] << 2) & 077];
}
else {
encodedBuf[destIndex++] = mBase64EncMap[(aData[srcIndex] << 4) & 077];
}
}
// Add padding to the end of encoded data
while (destIndex < encodedBuf.length) {
encodedBuf[destIndex] = (byte) '=';
destIndex++;
}
String result = new String(encodedBuf);
return result;
}
/**
* Decodes the given Base64-encoded data,
* as specified in RFC-2045 (Section 6.8).
*
* @param aData the Base64-encoded aData.
* @return the decoded <var>aData</var>.
* @exception IllegalArgumentException if NULL or empty data is passed
*/
public static byte[] base64Decode(String aData) {
if ((aData == null) || (aData.length() == 0))
throw new IllegalArgumentException("Can not decode NULL or empty string.");
byte[] data = aData.getBytes();
// Skip padding from the end of encoded data
int tail = data.length;
while (data[tail-1] == '=')
tail--;
byte decodedBuf[] = new byte[tail - data.length/4];
// ASCII-printable to 0-63 conversion
for (int i = 0; i < data.length; i++)
data[i] = mBase64DecMap[data[i]];
// 4-byte to 3-byte conversion
int srcIndex, destIndex;
for (srcIndex = 0, destIndex=0; destIndex < decodedBuf.length-2;
srcIndex += 4, destIndex += 3) {
decodedBuf[destIndex] = (byte) ( ((data[srcIndex] << 2) & 255) |
((data[srcIndex+1] >>> 4) & 003) );
decodedBuf[destIndex+1] = (byte) ( ((data[srcIndex+1] << 4) & 255) |
((data[srcIndex+2] >>> 2) & 017) );
decodedBuf[destIndex+2] = (byte) ( ((data[srcIndex+2] << 6) & 255) |
(data[srcIndex+3] & 077) );
}
// Handle last 1 or 2 bytes
if (destIndex < decodedBuf.length)
decodedBuf[destIndex] = (byte) ( ((data[srcIndex] << 2) & 255) |
((data[srcIndex+1] >>> 4) & 003) );
if (++destIndex < decodedBuf.length)
decodedBuf[destIndex] = (byte) ( ((data[srcIndex+1] << 4) & 255) |
((data[srcIndex+2] >>> 2) & 017) );
return decodedBuf;
}
}
아놔..고민고민 끝에 암호화된 대칭키를 아래 모듈의 DECODE로 풀어서 넣었더니..되더라..음..
public class Base64Utils {
private static byte[] mBase64EncMap, mBase64DecMap;
/**
* Class initializer. Initializes the Base64 alphabet (specified in RFC-2045).
*/
static {
byte[] base64Map = {
(byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E', (byte)'F',
(byte)'G', (byte)'H', (byte)'I', (byte)'J', (byte)'K', (byte)'L',
(byte)'M', (byte)'N', (byte)'O', (byte)'P', (byte)'Q', (byte)'R',
(byte)'S', (byte)'T', (byte)'U', (byte)'V', (byte)'W', (byte)'X',
(byte)'Y', (byte)'Z',
(byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f',
(byte)'g', (byte)'h', (byte)'i', (byte)'j', (byte)'k', (byte)'l',
(byte)'m', (byte)'n', (byte)'o', (byte)'p', (byte)'q', (byte)'r',
(byte)'s', (byte)'t', (byte)'u', (byte)'v', (byte)'w', (byte)'x',
(byte)'y', (byte)'z',
(byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5',
(byte)'6', (byte)'7', (byte)'8', (byte)'9', (byte)'+', (byte)'/' };
mBase64EncMap = base64Map;
mBase64DecMap = new byte[128];
for (int i=0; i<mBase64EncMap.length; i++)
mBase64DecMap[mBase64EncMap[i]] = (byte) i;
}
/**
* This class isn't meant to be instantiated.
*/
private Base64Utils() {
}
/**
* Encodes the given byte[] using the Base64-encoding,
* as specified in RFC-2045 (Section 6.8).
*
* @param aData the data to be encoded
* @return the Base64-encoded <var>aData</var>
* @exception IllegalArgumentException if NULL or empty array is passed
*/
public static String base64Encode(byte[] aData) {
if ((aData == null) || (aData.length == 0))
throw new IllegalArgumentException("Can not encode NULL or empty byte array.");
byte encodedBuf[] = new byte[((aData.length+2)/3)*4];
// 3-byte to 4-byte conversion
int srcIndex, destIndex;
for (srcIndex=0, destIndex=0; srcIndex < aData.length-2; srcIndex += 3) {
encodedBuf[destIndex++] = mBase64EncMap[(aData[srcIndex] >>> 2) & 077];
encodedBuf[destIndex++] = mBase64EncMap[(aData[srcIndex+1] >>> 4) & 017 |
(aData[srcIndex] << 4) & 077];
encodedBuf[destIndex++] = mBase64EncMap[(aData[srcIndex+2] >>> 6) & 003 |
(aData[srcIndex+1] << 2) & 077];
encodedBuf[destIndex++] = mBase64EncMap[aData[srcIndex+2] & 077];
}
// Convert the last 1 or 2 bytes
if (srcIndex < aData.length) {
encodedBuf[destIndex++] = mBase64EncMap[(aData[srcIndex] >>> 2) & 077];
if (srcIndex < aData.length-1) {
encodedBuf[destIndex++] = mBase64EncMap[(aData[srcIndex+1] >>> 4) & 017 |
(aData[srcIndex] << 4) & 077];
encodedBuf[destIndex++] = mBase64EncMap[(aData[srcIndex+1] << 2) & 077];
}
else {
encodedBuf[destIndex++] = mBase64EncMap[(aData[srcIndex] << 4) & 077];
}
}
// Add padding to the end of encoded data
while (destIndex < encodedBuf.length) {
encodedBuf[destIndex] = (byte) '=';
destIndex++;
}
String result = new String(encodedBuf);
return result;
}
/**
* Decodes the given Base64-encoded data,
* as specified in RFC-2045 (Section 6.8).
*
* @param aData the Base64-encoded aData.
* @return the decoded <var>aData</var>.
* @exception IllegalArgumentException if NULL or empty data is passed
*/
public static byte[] base64Decode(String aData) {
if ((aData == null) || (aData.length() == 0))
throw new IllegalArgumentException("Can not decode NULL or empty string.");
byte[] data = aData.getBytes();
// Skip padding from the end of encoded data
int tail = data.length;
while (data[tail-1] == '=')
tail--;
byte decodedBuf[] = new byte[tail - data.length/4];
// ASCII-printable to 0-63 conversion
for (int i = 0; i < data.length; i++)
data[i] = mBase64DecMap[data[i]];
// 4-byte to 3-byte conversion
int srcIndex, destIndex;
for (srcIndex = 0, destIndex=0; destIndex < decodedBuf.length-2;
srcIndex += 4, destIndex += 3) {
decodedBuf[destIndex] = (byte) ( ((data[srcIndex] << 2) & 255) |
((data[srcIndex+1] >>> 4) & 003) );
decodedBuf[destIndex+1] = (byte) ( ((data[srcIndex+1] << 4) & 255) |
((data[srcIndex+2] >>> 2) & 017) );
decodedBuf[destIndex+2] = (byte) ( ((data[srcIndex+2] << 6) & 255) |
(data[srcIndex+3] & 077) );
}
// Handle last 1 or 2 bytes
if (destIndex < decodedBuf.length)
decodedBuf[destIndex] = (byte) ( ((data[srcIndex] << 2) & 255) |
((data[srcIndex+1] >>> 4) & 003) );
if (++destIndex < decodedBuf.length)
decodedBuf[destIndex] = (byte) ( ((data[srcIndex+1] << 4) & 255) |
((data[srcIndex+2] >>> 2) & 017) );
return decodedBuf;
}
}
AES 256 암호화 오류
JDK1.4 : java.lang.SecurityException : UnSupported keysize or algorithm parameters
JDK1.5 : java.security.InvalidKeyException: Illegal key size or default parameters
첨부된 압축파일을 풀어서 local_policy.jar, US_export_policy.jar 파일을
%JAVA_HOME%\jre\lib\security 안에 복사하면 된다.
아..무진장 헤맸다..이것때문에..ㅡ_-;;
이 글을 포스팅 해 놓은 원문의 블로거님께 감사드립니다..(__);
[원문 : http://blog.naver.com/kimsumin75/20053382618]
라벨:
단호박업무,
aes 256,
aes256,
JAVA,
java aes 256
2010년 3월 18일 목요일
AJAX이용한 XML 데이터 전송 및 전송된 XML 데이터 받기
1.xmlPostClient.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest(){
if(window.ActiveXObject){
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest){
xmlHttp=new XMLHttpRequest();
}
}
function sendXML(){
createXMLHttpRequest();
var xml="<VCOUPON><TEL_NO>0101234567</TEL_NO><AMOUNT>1000</AMOUNT><RETURNCODE>00</RETURNCODE></VCOUPON>";
var url="http://localhost:8080/test/httpPostServer.jsp";
xmlHttp.open("POST",url,true);
xmlHttp.onreadystatechange=handleStateChange;
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
xmlHttp.setRequestHeader("aaa","aaa");
xmlHttp.setRequestHeader("bbb","bbb");
xmlHttp.send(xml);
}
function handleStateChange(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
parseResults();
}
}
}
function parseResults(){
var responseDiv=document.getElementById("response");
if(responseDiv.hasChildNodes()){
responseDiv.removeChild(responseDiv.childNodex[0]);
}
var responseText = document.createTextNode(xmlHttp.responseText);
responseDiv.appendChild(responseText);
}
</script>
</head>
<body>
<input type="button" value="보내기" onClick="sendXML();"><br>
<div id="response">
</div>
</body>
</html>
2.xmlPostServer.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ page import = "java.io.*" %>
<%@ page import = "javax.xml.parsers.DocumentBuilderFactory" %>
<%@ page import = "javax.xml.parsers.ParserConfigurationException" %>
<%@ page import = "org.w3c.dom.Document" %>
<%@ page import = "org.w3c.dom.NodeList" %>
<%@ page import = "org.xml.sax.SAXException" %>
<%!
public String readXMLFromRequestBody(HttpServletRequest request){
StringBuffer xml = new StringBuffer();
String line = null;
try{
BufferedReader reader = request.getReader();
while((line=reader.readLine())!=null){
xml.append(line);
}
}catch(Exception e){
System.out.println("Error reading XML:"+e.toString());
}
return xml.toString();
}
%>
<%
String xml=readXMLFromRequestBody(request);
Document xmlDoc = null;
try{
xmlDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes()));
}catch(ParserConfigurationException e){
System.out.println("ParserConfigurationException : "+e);
}catch(SAXException e){
System.out.println("SAXException : "+e);
}
System.out.println("header aaa : "+request.getHeader("aaa"));
System.out.println("header bbb : "+request.getHeader("bbb"));
NodeList TEL_NO = xmlDoc.getElementsByTagName("TEL_NO");
NodeList AMOUNT = xmlDoc.getElementsByTagName("AMOUNT");
NodeList RETURNCODE = xmlDoc.getElementsByTagName("RETURNCODE");
String telno = null;
String amount = null;
String returncode = null;
String responseText = "request text : ";
for(int i = 0; i < TEL_NO.getLength();i++){
telno = TEL_NO.item(i).getFirstChild().getNodeValue();
responseText = responseText+"TEL_NO : "+telno;
}
for(int i = 0; i < AMOUNT.getLength();i++){
amount = AMOUNT.item(i).getFirstChild().getNodeValue();
responseText = responseText+"AMOUNT : "+amount;
}
for(int i = 0; i < RETURNCODE.getLength();i++){
returncode = RETURNCODE.item(i).getFirstChild().getNodeValue();
responseText = responseText+"RETURNCODE : "+returncode;
}
response.setContentType("text/xml");
out.print(responseText);
%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest(){
if(window.ActiveXObject){
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest){
xmlHttp=new XMLHttpRequest();
}
}
function sendXML(){
createXMLHttpRequest();
var xml="<VCOUPON><TEL_NO>0101234567</TEL_NO><AMOUNT>1000</AMOUNT><RETURNCODE>00</RETURNCODE></VCOUPON>";
var url="http://localhost:8080/test/httpPostServer.jsp";
xmlHttp.open("POST",url,true);
xmlHttp.onreadystatechange=handleStateChange;
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
xmlHttp.setRequestHeader("aaa","aaa");
xmlHttp.setRequestHeader("bbb","bbb");
xmlHttp.send(xml);
}
function handleStateChange(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
parseResults();
}
}
}
function parseResults(){
var responseDiv=document.getElementById("response");
if(responseDiv.hasChildNodes()){
responseDiv.removeChild(responseDiv.childNodex[0]);
}
var responseText = document.createTextNode(xmlHttp.responseText);
responseDiv.appendChild(responseText);
}
</script>
</head>
<body>
<input type="button" value="보내기" onClick="sendXML();"><br>
<div id="response">
</div>
</body>
</html>
2.xmlPostServer.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ page import = "java.io.*" %>
<%@ page import = "javax.xml.parsers.DocumentBuilderFactory" %>
<%@ page import = "javax.xml.parsers.ParserConfigurationException" %>
<%@ page import = "org.w3c.dom.Document" %>
<%@ page import = "org.w3c.dom.NodeList" %>
<%@ page import = "org.xml.sax.SAXException" %>
<%!
public String readXMLFromRequestBody(HttpServletRequest request){
StringBuffer xml = new StringBuffer();
String line = null;
try{
BufferedReader reader = request.getReader();
while((line=reader.readLine())!=null){
xml.append(line);
}
}catch(Exception e){
System.out.println("Error reading XML:"+e.toString());
}
return xml.toString();
}
%>
<%
String xml=readXMLFromRequestBody(request);
Document xmlDoc = null;
try{
xmlDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes()));
}catch(ParserConfigurationException e){
System.out.println("ParserConfigurationException : "+e);
}catch(SAXException e){
System.out.println("SAXException : "+e);
}
System.out.println("header aaa : "+request.getHeader("aaa"));
System.out.println("header bbb : "+request.getHeader("bbb"));
NodeList TEL_NO = xmlDoc.getElementsByTagName("TEL_NO");
NodeList AMOUNT = xmlDoc.getElementsByTagName("AMOUNT");
NodeList RETURNCODE = xmlDoc.getElementsByTagName("RETURNCODE");
String telno = null;
String amount = null;
String returncode = null;
String responseText = "request text : ";
for(int i = 0; i < TEL_NO.getLength();i++){
telno = TEL_NO.item(i).getFirstChild().getNodeValue();
responseText = responseText+"TEL_NO : "+telno;
}
for(int i = 0; i < AMOUNT.getLength();i++){
amount = AMOUNT.item(i).getFirstChild().getNodeValue();
responseText = responseText+"AMOUNT : "+amount;
}
for(int i = 0; i < RETURNCODE.getLength();i++){
returncode = RETURNCODE.item(i).getFirstChild().getNodeValue();
responseText = responseText+"RETURNCODE : "+returncode;
}
response.setContentType("text/xml");
out.print(responseText);
%>
피드 구독하기:
글 (Atom)