第1楼 2008-04-03 07:45 zajize 写道:
求助:java中http网络延时问题,为什么一直跳不出去
请高手帮我看一下这个程序,读出URL中的内容,但是对于那些需要很长时间才能打开的页面,该程序几个小时都跳不出来,我已经设置了urlConnection.setConnectTimeout(3000);urlConnection.setReadTimeout(10000);但觉得根本就不起作用,希望高手解决一下
private String readURL(URL url){
if (url.getProtocol().compareTo("http") != 0)
return "";
try {
URLConnection urlConnection =(URLConnection) url.openConnection();
urlConnection.setConnectTimeout(3000);
urlConnection.setReadTimeout(10000);
InputStream urlStream = url.openStream();
String type = urlConnection.getContentType();
if (type == null)
return "";
type = type.substring(0,9);
if (type.compareTo("text/html") != 0)
return "";
byte b[] = new byte[1000];
int numRead = urlStream.read(b);
if(numRead==-1)
return "";
String content = new String(b, 0, numRead);
while (numRead != -1) {
numRead = urlStream.read(b);
if (numRead != -1) {
String newContent = new String(b, 0, numRead);
content += newContent;
}
}
urlStream.close();
return content;
}catch (IOException e) {
return "";
}