Android > HttpURLConnection - getContentType() ÇÑ±Û ÀÎÄÚµù (UTF-8, EUC-KR)
µî·ÏÀÏ : 2017-07-06 17:36
Á¶È¸¼ö : 53,072
À¥°ú °ü·ÃµÈ ÇÁ·Î±×·¥À» ±¸¼ºÇÒ ¶§, URLÀ» ÀÔ·ÂÇؼ ÇØ´ç ±ÛÀ» °¡Á®¿À´Â ·çƾÀÌ ÇÊ¿äÇÑ °æ¿ì°¡ Á¾Á¾ ÀÖ½À´Ï´Ù.
ÀÌ °æ¿ì, Ç×»ó ¹®Á¦°¡ µÇ´Â °ÍÀÌ ¹Ù·Î ÀÎÄÚµù ¹®Á¦Àä.
À¥»çÀÌÆ®ÀÇ °æ¿ì, ÀÎÄÚµùÀ» ÁÖ·Î UTF-8°ú EUC-KRÀÇ µÎ °¡Áö¸¦ »ç¿ëÇؼ ó¸®ÇÏ°í ÀÖ½À´Ï´Ù.
Çì´õÁ¤º¸¸¸ Àоî¿Í¼ ÀÎÄÚµùÀ» ÆľÇÇÏ´Â ¹æ½ÄÀ» ¼Ò°³ÇÏ°íÀÚ ÇÕ´Ï´Ù.
URL u = new URL(strUrl);
URLConnection uc = u.openConnection();
String headerType = uc.getContentType();
BufferedReader in;
if (headerType.toUpperCase().indexOf("EUC-KR") != -1) {
in = new BufferedReader(new InputStreamReader(uc.getInputStream(),"EUC-KR"));
} else if (headerType.toUpperCase().indexOf("UTF-8") != -1){
in = new BufferedReader(new InputStreamReader(uc.getInputStream(),"UTF-8"));
}
¼Ò½º¸¦ º¸¸é ContentTypeÀ» °¡Á®¿À´Â ºÎºÐÀÌ ÀÖ½À´Ï´Ù.
¿©±â¿¡ UTF-8ÀÎÁö EUC-KRÀÎÁö Á¤º¸°¡ Æ÷ÇԵǾî ÀÖ´ÂÁö¸¦ È®ÀÎÇÏ°í ÇØ´ç ÀÎÄÚµùÀ¸·Î ¹®¼¸¦ Àоî¿À´Â °ÍÀÔ´Ï´Ù.
ÀÌ¿Õ Á¤¸®ÇÑ °Í URLÀ» ¹®ÀÚ¿·Î ÁÖ¸é ÇØ´ç HTMLÀ» ¸ðµÎ Àоî¿Í¼ ¸®ÅÏÇØÁÖ´Â ÇÔ¼ö¸¦ ¿Ã¸³´Ï´Ù.
ContentType¿¡ ÀÎÄÚµù Á¤º¸°¡ ¾øÀ» °æ¿ìµµ EUC-KR·Î ¼±Á¤Çϵµ·Ï ÄÚµùÇß½À´Ï´Ù.
private String getUrlContent(String strUrl) throws Exception {
URL u = new URL(strUrl);
URLConnection uc = u.openConnection();
uc.setDoOutput(true);
uc.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
String headerType = uc.getContentType();
BufferedReader in;
if (headerType.toUpperCase().indexOf("UTF-8") != -1){
in = new BufferedReader(new InputStreamReader(uc.getInputStream(),"UTF-8"));
} else {
in = new BufferedReader(new InputStreamReader(uc.getInputStream(),"EUC-KR"));
}
StringBuffer sb = new StringBuffer();
String thisLine = null;
while( (thisLine = in.readLine())!=null ){
sb.append(thisLine);
sb.append("\n");
}
in.close();
return sb.toString();
}