请高手帮我看一下这个程序,读出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 "";
}
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 "";
}
你好像是用URL方法的public abstract void connect()URLConnection类的connect这个方法才会对超时进行管理的,你用URL类流连接,它当然不会对你在URLConnection类设置的超时进行管理啦。
有内有计时器了,呵呵,再写多浪费呀,呵呵
private String readURL(URL url)
{ if (url.getProtocol().compareTo("http") != 0)
return doFailCase(); HttpURLConnection httpConnection = null;// 这里可以定义成HttpURLConnection
InputStream urlStream = null; try
{
httpConnection = (HttpURLConnection) url.openConnection();
httpConnection.setConnectTimeout(3000);
httpConnection.setReadTimeout(10000); httpConnection.connect();
// urlConnection.getContentEncoding();可以根据字符集设置接收到字符串的编码 urlStream = httpConnection.getInputStream();
if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK)
{
String type = httpConnection.getContentType();
if (type == null)
return doFailCase();
type = type.substring(0, 9);
if (type.compareTo("text/html") != 0)
return doFailCase();
byte b[] = new byte[1000];
int numRead = urlStream.read(b);
if (numRead == -1)
return doFailCase();
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;
}
}
return content;
}
else
{
return doFailCase();
}
}
catch (IOException e)
{
return doFailCase();
}
finally
{
if (null != urlStream)
{
try
{
urlStream.close();
}
catch (IOException e)
{
}
}
}
} private String doFailCase()
{
return "";
}
InputStream urlStream = url.openStream();
换成
InputStream urlStream = urlConnection.getInputStream();
,个人认为你设置了urlConnection的连接时间,又不用urlConnection去获取输入流,所以程序会一直等待下去
URLConnection urlConnection =(URLConnection) url.openConnection();
InputStream urlStream = urlConnection.openStream(); openConnection() ---》 connect()