java定时器的问题,程序隔一段时间发送一次HTTP请求,但是....

liuxiaohui1987 2010-03-26 04:43:06
做了一个定时器,就是每个一段时间去发送一个HTTP请求,程序:

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Timer;
import java.util.TimerTask;

public class HttpTimerTest {

public static void sendHttp(String urlStr) {
System.out.println("sendHttp");
try {
URL url = new URL(urlStr);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setDoOutput(true);

int size = httpURLConnection.getContentLength();
byte[] buffer = new byte[size];
byte[] in_b = new byte[size];
int count = 0;
int rbyte = 0;
System.out.println("size=" + size);
InputStream is = httpURLConnection.getInputStream();
while (count < size) { // 循环读取
rbyte = is.read(buffer);
for (int i = 0; i < rbyte; i++) {
in_b[count + i] = buffer[i];
}
count += rbyte;
}
String content = new String(buffer, "UTF-8");
System.out.println(content);
is.close();
} catch (Exception e) {
e.printStackTrace();
}

}

public static void main(String args[]) {
Timer timer = new Timer();
boolean i = true;
timer.schedule(new URLTask(), 1000, 5000);
while (i) {
System.out.println(i);
try {
int ch = System.in.read();
if (ch - 'c' == 0) {
i = false;
timer.cancel();//使用这个方法退出任务
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

static class URLTask extends TimerTask {

public void run() {
long start = System.currentTimeMillis();
sendHttp("http://localhost:8888/1/1.txt");
//System.out.println("________");
long end = System.currentTimeMillis();
long time = end - start;
System.out.println(time / 1000 + "秒" + time % 1000 + "毫秒");
}
}
}

任务就是执行sendHttp(..),可是执行的时候好像就死在了sendHttp()里面了,应该是每隔5秒打印一次返回的内容,可是没打印,当退出的时候才打印一次,跟踪发现就到了sendHttp()就不走了。
但是我要是注释掉sendHttp,任务是只打印“-------”,正常,每隔5秒打印一次。
不知道为什么,高手来指点一下。
不胜感激,谢谢
...全文
887 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuxiaohui1987 2010-03-29
  • 打赏
  • 举报
回复
自己顶啊!
liuxiaohui1987 2010-03-26
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 marf_cn 的回复:]
int ch = System.in.read(); 你这里为什么要设置手动输入呢?
[/Quote]
那我怎么让他什么时候停就什么时候听呢?
liuxiaohui1987 2010-03-26
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 marf_cn 的回复:]
InputStream is = httpURLConnection.getInputStream();
while (count < size) { // 循环读取
rbyte = is.read(buffer);
这里出了问题,程序要求手动输入内容,输入后有了打印
[/Quote]
那每隔5秒sendHttp()都执行一次吗?
为什么我发现到了
int size = httpURLConnection.getContentLength();
就不走了呢?
marf_cn 2010-03-26
  • 打赏
  • 举报
回复
int ch = System.in.read(); 你这里为什么要设置手动输入呢?
marf_cn 2010-03-26
  • 打赏
  • 举报
回复
用这个函数替换你的sendhttp,导入commons-httpclient包

public static String httpGet(String url) {
String result = null;
HttpClient httpClient = new HttpClient();
GetMethod getMethod = new GetMethod(url);
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler());
try {
int statusCode = httpClient.executeMethod(getMethod);// 执行getMethod
System.out.println("status: "+statusCode);
if (statusCode != HttpStatus.SC_OK) {
System.out.println(getMethod.getStatusLine());
}
result = getMethod.getResponseBodyAsString();
System.out.println("url: "+url);
System.out.println("result: "+result);
// System.out.println("result: "+new String(result,"utf-8"));
} catch (HttpException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getCause());
} finally {
getMethod.releaseConnection(); // 释放连接
}
return new String(result).trim();
}
marf_cn 2010-03-26
  • 打赏
  • 举报
回复
InputStream is = httpURLConnection.getInputStream();
while (count < size) { // 循环读取
rbyte = is.read(buffer);
这里出了问题,程序要求手动输入内容,输入后有了打印
liuxiaohui1987 2010-03-26
  • 打赏
  • 举报
回复

int size = httpURLConnection.getContentLength();


就走到这一句就不打印了,知道退出后才打印
是不是跟线程有关?
来指点一下。
liuxiaohui1987 2010-03-26
  • 打赏
  • 举报
回复
自己顶一个。

51,399

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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