| |
1. UTF-8인 경우
String encStr = method.getResponseBodyAsString();
2. 인코딩 체크한다.
public static String EnCodeCheck(String contentall) throws IOException {
String content = contentall.substring(0, contentall.length());
int index = content.indexOf("encoding");
if (index == -1)
return "UTF-8";
int index1 = content.indexOf("=", index);
int index2 = content.indexOf("?>", index);
String encoding = content.substring(index1 + 1, index2); // "UTF-8"
encoding = encoding.replace('\"', ' ').trim();
return encoding;
}
3. 인코딩 값이 "UTF-8" 일 경우 아래와 같이 변환합니다.
String resultStr = new String(encStr.getBytes("8859_1"), "UTF8");
responseBody = resultStr.getBytes()
4. "UTF-8"이 아닐 경우 getResponseBody()메소드로 byte[] 를 반환합니다.
responseBody = method.getResponseBody();
String encStr = method.getResponseBodyAsString();
String encode = EnCodeCheck(encStr);
System.out.println(">>>>>>>>>>>>>>encode="+encode);
String resultStr = new String(encStr.getBytes("8859_1"), "UTF8");
byte[] responseBody = resultStr.getBytes();
|