vickzhu

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  151 随笔 :: 0 文章 :: 34 评论 :: 0 Trackbacks
以下都是实战经验:
1、Socket读取
      String strServer=http://www.google.cn;//这里同样可以用ip来访问:203.208.35.100
      String strPage="/language_tools?hl=zh-CN";
      try { 
           String hostname = strServer; 
           int port = 80; 
           InetAddress addr = InetAddress.getByName(hostname); 
           Socket socket = new Socket(addr, port);
           BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8")); 
           wr.write("GET " + strPage + " HTTP/1.0\r\n"); 
           wr.write("HOST:" + strServer + "\r\n"); 
           wr.write("\r\n"); 
           wr.flush();
           BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream()));
           String line; 
           while ((line = rd.readLine()) != null) { 
                System.out.println(line); 
           }
           wr.close(); 
           rd.close(); 
      } catch (Exception e) { 
           System.out.println(e.toString()); 
      }
2、HttpClient方式
      HttpClient client=new HttpClient();
      GetMethod method=new GetMethod("http://www.baidu.com/");
      int status=client.executeMethod(method);
      if(status==HttpStatus.SC_OK){
       //读取内容
       byte[] responseBody = method.getResponseBody();
       //处理内容
       System.out.println(new String(responseBody));
       System.out.println("文件名称:"+method.getPath());
        }
3、HttpURLConnection方式
      URL url = new URL("这里是你要连接的地址");
      HttpURLConnection conn = (HttpURLConnection)url.openConnection();
      conn.setDoOutput(true);//是否可用于输出(输出参数),默认为fasle。另:setDoInput()为是否可用于输入,默认为true
      String parameters = "name=admin&password=123456";//这里是要传递的参数
      OutputStream os = conn.getOutputStream();
      os.write(parameters.getBytes("utf-8"));
      os.flush();
      os.close();
      System.out.println("返回状态码:"+conn.getResponseCode());
      System.out.println("返回消息:"+conn.getResponseMessage());
      InputStream is = conn.getInputStream();
      BufferedReader br = new BufferedReader(new InputStreamReader(is,"utf-8"));
      String line = null;
      while((line=br.readLine())!=null){
       System.out.println(line);
      }
    //  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();;
    //  DocumentBuilder db = dbf.newDocumentBuilder();
    //  Document doc = db.parse(is);
如果谁还有更多的方式分享,请留言!
posted on 2008-11-12 10:12 筱 筱 阅读(973) 评论(2)  编辑  收藏

评论

# re: java 读取页面源码 的多种方式[未登录] 2008-11-24 20:39 fisher
GetMethod method=new GetMethod("http://www.baidu.com/");

GetMethod ?

这个东西哪来的?  回复  更多评论
  

# re: java 读取页面源码 的多种方式 2008-11-25 09:03 vickzhu
你好HttpClient和GetMethod都是来自apache的包  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: