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”这个位置,数据流就结束了,但是实际上网页后面还有内容,下载过程中也没有超时,那位高手帮忙分析一下这是怎么回事?
String msg = "";
while ((msg = br.readLine())!=null) {
System.out.println(msg);
} 好可能是你那个网页有问题...
这个我问的就是这个网页怎么样才能下载全。按理讲,ie能正确打开的,java没有理由下载不下来啊。
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[])方法
//读取指定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;
}
/*
//直接把结果打印出来
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本身就是基于文本的。除非你读取的是二进制流。
别说我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这里。所以,应该和读缓冲区的大小没有什么关系。
当然,读超时这种情况,我也试过了。被排除。
我在想,是否是因为楼主提交的页面请求的内容中少了什么,而导致服务端程序不将完整的页面发送过来?