package modelframeworkdemo;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;public class CSHtml {
void display() {
try {
String addr = "http://www.pw.utc.com/vgn-ext-templating/v/PWSearch?keyWord=engine";
// 将用户输入的URL字符串传入URL类对象
URL url = new URL(addr);
// 创建URLConnection对象,用URL的openConnection方法将连接通过返回给URLConnection的对象
// 实际上URL的openConnection的返回值就是一个URLConnection
URLConnection c = url.openConnection(); // *
// 用URLConnection的connect()方法建立连接
c.connect(); // *
// 显示该连接的相关信息,这些都是URLConnection的方法
System.out.println("内容类型: " + c.getContentType());
System.out.println("内容长度: " + c.getContentLength());
System.out.println("创建日期: " + new Date(c.getDate()));
System.out.println("最后修改日期: " + new Date(c.getLastModified()));
System.out.println("终止日期: " + new Date(c.getExpiration())); InputStream is = c.getInputStream(); // *
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
char ch;
System.out.println("字节流长度: " + br.toString().length());
int msg = 0;   
                         int i = 0;
while ((msg = br.read()) != -1)   {  
                                System.out.println(msg + "   " + (char)msg);   
                        } 
System.out.println(br.read());
br.close();
} catch (Exception e) {
System.out.println(e);
}
} public static void main(String[] args) {
CSHtml app = new CSHtml();
app.display();
}
}
运行上面的代码,会发现下载到网页的“<option selected='select”这个位置,数据流就结束了,但是实际上网页后面还有内容,下载过程中也没有超时,那位高手帮忙分析一下这是怎么回事?

解决方案 »

  1.   

    我试用了你的代码是没有问题的...还找了一个有下拉框的页面.只改了这个:
    String  msg = "";   
    while ((msg = br.readLine())!=null)   {  
         System.out.println(msg);   
    } 好可能是你那个网页有问题...
      

  2.   


    这个我问的就是这个网页怎么样才能下载全。按理讲,ie能正确打开的,java没有理由下载不下来啊。
      

  3.   

    试了,和我这个函数的效果一样,读到“ <option selected='select”这个位置,数据流就断掉了。
      

  4.   

    给你那个网页的源代码来看看..特别是“ <option selected='select”这个位置在我这里试试...
      

  5.   

    catch也没有捉到什么错误吗?
      

  6.   

    BufferedReader.java的代码如下public int read() throws IOException {
    synchronized (lock) {
        ensureOpen();
        for (;;) {
    if (nextChar >= nChars) {
        fill();
        if (nextChar >= nChars)
        {
         System.out.println("nextChar = " + nextChar);
         System.out.println("nChars = " + nChars);
         return -1;
        }
    }
    if (skipLF) {
        skipLF = false;
        if (cb[nextChar] == '\n') {
    nextChar++;
    continue;
        }
    }
    return cb[nextChar++];
        }
    }
        }返回-1的时候nChars和nextChar均为8192,说明和读取时的字符缓存有关
    服务器中定义的的缓存为8192,所以最多stream中最多也就这么多个字符
    maxHttpHeaderSize="8192"
    你可以试着用read(char[])方法
      

  7.   

    首先不建议你直接使用HttpURLConnection处理读取网页应用,经过我的测试当网络中存在某些路由或者网络映射的时候,HttpURLConnection的速度偏慢。如下一份代码是我以前的老代码(使用了HttpURLConnection),你可以参考。
        //读取指定url的内容并转换为xml文档
        public Document doQuery1(String url)throws Exception{
            URL u = new URL(url);
    HttpURLConnection conn = (HttpURLConnection)(u.openConnection()); //
    conn.setDoOutput(false);
    conn.setDoInput(true);
    conn.setUseCaches(false);
    conn.setRequestMethod("GET");//GET
    conn.connect();

    int code = conn.getResponseCode();
    if(code!=HttpURLConnection.HTTP_OK){
        throw new Exception("远程没有返回正确结果,返回【"+code+"】。");
    }     
        
    //反馈..
    /*
    //直接把结果打印出来
    InputStream in = conn.getInputStream();
    BufferedReader br=new BufferedReader(new InputStreamReader(in));
    String t=null;
    while((t=br.readLine())!=null){
        System.out.println(t);
    }
    return null;
    */

    //正常解析
    InputStream in = conn.getInputStream();
    Document doc=Sys.loadXML(in);
    return doc;
        }
      

  8.   

    首先你的代码中没有任何的是否读取完成的判断,然后就是HttpURLConnection 远程获得的内容是不一定有长度的。注意:只有少部分网页会在响应头上说明本网页的长度,绝大部分都没有,因此你在程序中读取的长度实际上是缓冲区的长度,无效的。而我的代码中的读取部分
    /*
        //直接把结果打印出来
        InputStream in = conn.getInputStream();        
        
        BufferedReader br=new BufferedReader(new InputStreamReader(in));
        String t=null;
        while((t=br.readLine())!=null){
            System.out.println(t);
        }
        
        return null;
    */采用的是读文本行文件的方式,这是符合html的处理习惯,一般不建议你用字节读取,太麻烦。因为web本身就是基于文本的。除非你读取的是二进制流。
      

  9.   

    我并不认为楼主的方法有什么不妥,担心是缓冲区大小的原因,所以,换了个方法读。
    别说我BT,这样可以不用考虑编码问题,如果有乱码那就修改控制台的编码和网页编码相同就行了。
    import java.io.InputStream;
    import java.net.URL;
    import java.net.URLConnection;public class CSHtml {
        void display() {
            try {
                String addr = "http://www.pw.utc.com/vgn-ext-templating/v/PWSearch?keyWord=engine";
                URL url = new URL(addr);
                URLConnection c = url.openConnection(); // *
                c.connect(); // *
                InputStream is = c.getInputStream();
                byte [] b = new byte[102400];
                int i=0;
                while((i=is.read(b,0,b.length))!=-1){
                 System.out.write(b, 0, i);
                }
                is.close();
            } catch (Exception e) {
                System.out.println(e);
            }
        }    public static void main(String[] args) {
            CSHtml app = new CSHtml();
            app.display();
        }
    }
    换了几个缓冲区的值,但是,仍然卡在select这里。所以,应该和读缓冲区的大小没有什么关系。
    当然,读超时这种情况,我也试过了。被排除。
    我在想,是否是因为楼主提交的页面请求的内容中少了什么,而导致服务端程序不将完整的页面发送过来?