想要做一个程序,用URL访问一个网页,然后分析这个网页的HTML代码 (不是做Web Service)应该使用什么类,如何编成?

解决方案 »

  1.   

    直接用Socket的TCP连接服务器的IP以及web端口,然后给他发送HTTP命令(网上到处都有)。服务器会给你回应,其中就包括了HTML得源码。
      

  2.   

    http://www.zeali.net/blog/entry.php?id=70
      

  3.   

    //代码如下public class UrlTest {
        public static void main(String args[]) {
            String sUrl = "http://community.csdn.net/Expert/topic/4106/4112392.xml?temp=.9519617";  //假设这个网址提供一个xml文件
            BufferedReader reader = null;
            try {
                URL su = new URL(sUrl);            URLConnection uc = su.openConnection();
                StringBuffer sb = new StringBuffer();
                Reader r = new InputStreamReader(su.openStream());
                reader = new BufferedReader(r);
                String s = null;
                do {
                    s = reader.readLine();
                    if(s!=null){
                        sb.append(s).append("\r\n");
                    }
                } while (s != null);
                System.out.println(sb.toString());  //打印出xml
                
                //TODO 解析xml文件,这里略过
                
            } catch (IOException e1) {
                e1.printStackTrace();
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }