본문 바로가기
IT/JAVA

Java http, https URL 접근 및 response xml data 읽기

by 최고영회 2013. 12. 21.
728x90
반응형
SMALL
HashMap<String, String> rtnMap = new HashMap<String, String>();
URL url;
HttpURLConnection connection = null;
HttpsURLConnection conn = null;
BufferedReader br = null;
StringBuffer strXMLbuf = new StringBuffer();
try{
String strUrl = SystemEnvironment.getInstance().getEnviroment().getOtpUrl();
InputStream in = null;
if(strUrl.indexOf("https://") > -1) // https 통신(ssl)
{
url = new URL(SystemEnvironment.getInstance().getEnviroment().getOtpUrl()+"?userId="+(URLEncoder.encode(userId, "utf-8"))+"&otpValue="+otpVal);
conn = (HttpsURLConnection)url.openConnection();  
// Set Hostname verification  
conn.setHostnameVerifier(new HostnameVerifier() {  
@Override
public boolean verify(String arg0, SSLSession arg1) { return true; }  
});  
// Post 방식 
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
// SSL setting  
SSLContext context = SSLContext.getInstance("TLS");  
context.init(null, new TrustManager[] { new X509TrustManager() {  
@Override
public void checkClientTrusted( java.security.cert.X509Certificate[] arg0, String arg1) throws java.security.cert.CertificateException {
}
@Override
public void checkServerTrusted( java.security.cert.X509Certificate[] arg0, String arg1) throws java.security.cert.CertificateException {
}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null;}  
} }, null);  
conn.setSSLSocketFactory(context.getSocketFactory());  
// Connect to host
conn.connect();  
conn.setInstanceFollowRedirects(true);  
in = conn.getInputStream();  
}
else // http 통신
{
url = new URL(strUrl+"?userId="+(URLEncoder.encode(userId, "utf-8"))+"&otpValue="+otpVal);
connection = (HttpURLConnection)url.openConnection();
// Post 방식 
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
in = connection.getInputStream();
}
br = new BufferedReader(new InputStreamReader(in, "euc-kr"));
String buf = "";
int tmpIdx = 0;
while((buf = br.readLine()) != null){
if(buf == null) break;
strXMLbuf.append(buf).append("\n");
tmpIdx ++;
if(tmpIdx > 100)break; // 무한루프 막기 위함
}
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
org.w3c.dom.Document doc = builder.parse(new InputSource(new StringReader(strXMLbuf.toString())));
doc.getDocumentElement().normalize();
rtnMap.put("resultCode", doc.getElementsByTagName("resultCode").item(0).getChildNodes().item(0).getNodeValue());
rtnMap.put("resultMsg", doc.getElementsByTagName("resultMsg").item(0).getChildNodes().item(0).getNodeValue());
// result xml data 는 아래와 같다.
// <xml><resultCode>0</resultCode><resultMsg>성공</resultMsg></xml>
} catch (Exception e) {
logger.error("checkOTP Exception ["+e.getMessage()+"]");
return null;
} finally{
try{
if(br != null) br.close();
if(conn != null) conn.disconnect();
if(connection != null) connection.disconnect();
}catch(Exception e){
e.printStackTrace();
}
}


728x90
반응형
LIST

'IT > JAVA' 카테고리의 다른 글

properties file 한글 깨짐 현상 해결 방법  (0) 2013.12.21
java unsigned int  (0) 2013.12.21
Java 객체 직렬화  (0) 2013.12.21
Java ProcessBuilder, Runtime,, linux 에서 사용하기  (0) 2013.12.21
NIO 이용 파일 카피  (0) 2013.12.21