Code
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.*;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
* Created by linux_ccmj on 16-5-1.
*/
public class GetBaidu {
/* Main */
public static void main(String[] args) {
System.out.println("获取页面数据");
getConByLan("zh-cn");
System.out.println("\n");
}
/* Get content function */
public static void getConByLan(String con) {
try {
/* URL, Property */
URL urlBaidu = new URL("http://www.baidu.com");
HttpURLConnection conUrlBaidu = (HttpURLConnection) urlBaidu.openConnection();
conUrlBaidu.setRequestProperty("Accept-Language", con);
/* Get property */
Map req = conUrlBaidu.getRequestProperties();
Set fieReq = req.keySet();
Iterator itrReq = fieReq.iterator();
while (itrReq.hasNext()) {
String strFie = (String) itrReq.next();
System.out.println(strFie + " : " + conUrlBaidu.getRequestProperty(strFie));
}
System.out.println("\n");
/* Get Header */
Map res = conUrlBaidu.getHeaderFields();
Set fieRes = res.keySet();
Iterator itrRes = fieRes.iterator();
while (itrRes.hasNext()){
String strFie = (String) itrRes.next();
System.out.println(strFie + " : " + conUrlBaidu.getHeaderField(strFie));
}
System.out.println("\n");
//conUrlBaidu.connect();
/* Get Content */
InputStream strInp = conUrlBaidu.getInputStream();
BufferedReader buf = new BufferedReader(new InputStreamReader(strInp));
String strLin = null;
while ((strLin=buf.readLine())!=null){
System.out.println(strLin + "\n");
}
buf.close();
}
catch (Exception e){
e.printStackTrace();
}
}
}
2. Demo