package january;
import java.io.*;
import java.net.*;
public class WebPageCharSet {
private static final int DEFAULT = 1;
private static final int ISOLATIN1 = 2;
private static final int UTF8 = 3;
private static final int AUTODETECT = 4;
private static String get(String urlstr, int cs) {
StringBuffer sb = new StringBuffer();
try {
URL url = new URL(urlstr);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.connect();
if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream is = con.getInputStream();
byte[] b = new byte[1000];
int n;
while ((n = is.read(b)) >= 0) {
switch(cs) {
case DEFAULT:
sb.append(new String(b, 0, n));
break;
case ISOLATIN1:
sb.append(new String(b, 0, n, "ISO8859-1"));
break;
case UTF8:
sb.append(new String(b, 0, n, "UTF-8"));
break;
case AUTODETECT:
sb.append(new String(b, 0, n, con.getContentType().substring("text/html; Charset=".length())));
break;
}
}
is.close();
}
con.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
private static void test(boolean isActualUTF8, boolean isLabeledUTF8) {
System.out.println("Actual: " + (isActualUTF8 ? "UTF-8" : "ISO8859-1"));
System.out.println("Labeled: " + (isLabeledUTF8 ? "UTF-8" : "ISO8859-1"));
String urlstr = "
http://localhost/" + (isActualUTF8 ? "utf8" : "isolatin1") + ".asp?cs=" + (isLabeledUTF8 ? "utf-8" : "iso8859-1");
System.out.println("Result (default): " + get(urlstr, DEFAULT).contains("ÆØÅæøå"));
System.out.println("Result (ISO8859-1): " + get(urlstr, ISOLATIN1).contains("ÆØÅæøå"));
System.out.println("Result (UTF-8): " + get(urlstr, UTF8).contains("ÆØÅæøå"));
System.out.println("Result (auto detect): " + get(urlstr, AUTODETECT).contains("ÆØÅæøå"));
}
public static void main(String[] args) {
test(true, true);
test(true, false);
test(false, true);
test(false, false);
}
}