实现代码:
import java.net.*;
import java.util.*;
import java.io.*;
public class GetGoogle {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
System.out.println("获取日文页面");
getContentByLanguage("ja");
System.out.println("\n");
System.out.println("获取中文页面");
getContentByLanguage("zh-cn");
}
public static void getContentByLanguage(String country) throws Exception
{
URL urlGoogle = new URL("http://www.google.com");
HttpURLConnection googleConnection =
(HttpURLConnection)urlGoogle.openConnection();
googleConnection.setRequestProperty("Accept-Language",country);
Map requests = googleConnection.getRequestProperties();
Set reqFields = requests.keySet();
Iterator itrReq = reqFields.iterator();
while (itrReq.hasNext()) {
String field = (String)itrReq.next();
System.out.println(field + ":" +
googleConnection.getRequestProperty(field));
}
googleConnection.connect();
Map responses = googleConnection.getHeaderFields();
Set resFields = responses.keySet();
Iterator itrRes = resFields.iterator();
while (itrRes.hasNext()) {
String field = (String)itrRes.next();
System.out.println(field + ":" +
googleConnection.getHeaderField(field));
}
InputStream is = googleConnection.getInputStream();
BufferedReader br = new BufferedReader(
new InputStreamReader(is));
String strLine = null;
while ((strLine = br.readLine()) != null) {
System.out.println(strLine);
}
br.close();
googleConnection.disconnect();
}
}