×

Loading...
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务

为什么用java读,网页的charset=iso-8859_1, 网页上的汉字为乱码,直接用浏览器下载,网页的charset=utf8? 有经验的帮我分析一下。URL: http://babelfish.yahoo.com/translate_txt

本文发表在 rolia.net 枫下论坛Java code:

..........................................
..........................................

URL url;
URLConnection urlConn;
DataOutputStream printout;

BufferedReader input;

url = new URL("http://babelfish.yahoo.com/translate_txt");

urlConn = url.openConnection();
urlConn.setConnectTimeout(0);

urlConn.setDoInput(true);
urlConn.setDoOutput(true);
urlConn.setUseCaches(false);
urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
urlConn.setRequestProperty("content-type", "text/html;charset=utf-8");

// Send POST output.
printout = new DataOutputStream(urlConn.getOutputStream());
String content =
"trtext=" + URLEncoder.encode(msg, "UTF8") +
"&lp=" + URLEncoder.encode(lang, "UTF8");

System.out.println("trtext=" + msg);
System.out.println("&lp=" + lang);

printout.write(content.getBytes(), 0, content.getBytes().length);
printout.flush();
printout.close();

input = new BufferedReader ( new InputStreamReader( urlConn.getInputStream (), "UTF8" ) );

String str, str_1;
int i = 0;
str = "not available";

while (null != (str_1 = input.readLine())) {

str = str_1;
System.out.println(str);
}

........................
........................更多精彩文章及讨论,请光临枫下论坛 rolia.net
Report