黑马程序员技术交流社区
标题:
急急急?求解,URLConnection类与HTTPURLConnection类
[打印本页]
作者:
陈俊来
时间:
2012-9-16 18:15
标题:
急急急?求解,URLConnection类与HTTPURLConnection类
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class GetGoogle {
public static void main(String[] args) throws IOException {
System.out.println("获取日文页面");
getContentByLanguage("ja");
System.out.println("\n");
System.out.println("获取中文页面");
getContentByLanguage("zh-cn");
}
public static void getContentByLanguage(String country) throws IOException {
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 ireReq=reqFields.iterator();
while(ireReq.hasNext()){
String field=(String)ireReq.next();
System.out.println(field+": "+
googleConnection.getRequestProperty(field));
}
googleConnection.connect();
Map responses=googleConnection.getHeaderFields();
Set resFields=responses.keySet();
Iterator ireRes=resFields.iterator();
while(ireRes.hasNext()){
String field=(String)ireRes.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();
}
}
我是按视频上一步一步来的,为什么运行的时候,获取日文页面没有问题,获取中文页面报错呀??
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2