Java之利用Socket获取网站内容

public void main(String[] args){
  String host = "123.126.113.42";
  int port = 80;
  Socket s = new Socket(host, port);
  BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream(), "UTF8")); 
  BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
  StringBuffer sb = new StringBuffer();
  bw.write("GET / HTTP/1.1\r\n"); //get关键字必须大写
  bw.write("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\n");
  bw.write("Accept-Encoding:gzip,deflate,sdch\r\n");
  bw.write("Accept-Language:zh-CN,zh;q=0.8\r\n");
  bw.write("Connection: close\r\n");
  bw.write("Host: www.sogou.com\r\n");
  bw.write("User-agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36 SE 2.X MetaSr 1.0\r\n");
  bw.write("\r\n");
  bw.flush(); //记得添加此操作
  String lineContent = null;
  while((lineContent = br.readLine()) != null){
    System.out.println(lineContent);
  }
  br.close();
  bw.close();
  s.close();
}
 
}
上一篇:Linux基础-正则表达式整理---------------grep、sed、awk


下一篇:spoj - Grass Planting(树链剖分模板题)