求助:java中http网络延时问题,为什么一直跳不出去

zajize 2008-04-03 01:03:50
请高手帮我看一下这个程序,读出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 "";
}
...全文
254 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
hmsuccess 2008-04-03
  • 打赏
  • 举报
回复

URLConnection urlConnection =(URLConnection) url.openConnection();
InputStream urlStream = urlConnection.openStream();

openConnection() ---》 connect()
zajize 2008-04-03
  • 打赏
  • 举报
回复
可以了 再次谢谢大家 我去结贴
zajize 2008-04-03
  • 打赏
  • 举报
回复
谢谢大家了,我正在改
zhmt 2008-04-03
  • 打赏
  • 举报
回复
楼上说的没错,楼主太大意了
jxcfh 2008-04-03
  • 打赏
  • 举报
回复
或者把
InputStream urlStream = url.openStream();

换成
InputStream urlStream = urlConnection.getInputStream();

,个人认为你设置了urlConnection的连接时间,又不用urlConnection去获取输入流,所以程序会一直等待下去
zajize 2008-04-03
  • 打赏
  • 举报
回复
对网络编程不是特别懂,具体应该怎么改呀,能不能帮帮忙给改一下呀
jxcfh 2008-04-03
  • 打赏
  • 举报
回复

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 "";
}
chensjmail 2008-04-03
  • 打赏
  • 举报
回复
URLConnection
有内有计时器了,呵呵,再写多浪费呀,呵呵
chensjmail 2008-04-03
  • 打赏
  • 举报
回复
url.openStream();
你好像是用URL方法的

public abstract void connect()

URLConnection类的connect这个方法才会对超时进行管理的,

你用URL类流连接,它当然不会对你在URLConnection类设置的超时进行管理啦。

gaow_2005 2008-04-03
  • 打赏
  • 举报
回复
那就自己写一个计时器吧!时间到了就跳出
zs_han 2008-04-03
  • 打赏
  • 举报
回复
UP

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧