使用httpclient 如何多线程重复读取某个页面,然后判断页面是否发生改变了呢?ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager();
        cm.setMaxTotal(100);
        cm.setDefaultMaxPerRoute(5);
httpclient = new DefaultHttpClient(cm);
Thread th1 = new Thread(){
public void run(){
ExecutorService executor = Executors.newCachedThreadPool();
for(int i=0;i<10;i++){
executor.execute(new exe_post());
}

executor.shutdown();
while(!executor.isTerminated()){

}
System.out.println("Thread1 is end");
}};exe_post类如下:class exe_post implements Runnable{
public void run(){
try {
                        HttpPost httpost = new HttpPost("http://www.xxx.com");
                         ......
                         ......
httpclient.execute(httpost);
                        HttpEntity entity = response.getEntity();
                        判断entity内容
} catch (Exception e) {
e.printStackTrace();
}
}
}
我是初学者,希望大侠、高手给予指导!
以上是我写的代码,错误一大堆啊
我的思路是这样的:
多线程并发去POST同一页面,对返回的结果页面进行分析,
如果结果页面包含某一关键字符,就停止。
否则继续重新读取这个要怎么写呢?

解决方案 »

  1.   

    楼主在搞TDB的订票网站啊?entity 取出HTML的String,然后直接用 String.indexOf("关键字") 就行了吧。
    另外,考虑到并发问题,你应该每个线程用一个独立的HttpClient。
      

  2.   

    重新看了下你的代码,发现逻辑结构有点混乱啊。建议你的程序分为两个部分:
    1、主程序,负责启动工作线程;
    2、工作线程,负责执行全过程操作。工作线程建议采用有穷自动机模型,用一个状态来跟踪整个业务流程,比如:首先是登录,然后是刷票,接下来是XXOO。前一个状态成功了,就开始处理下一个环节。伪代码模型如下:HttpClient client = new XXOO;
    int status = 0;
    public void run() {
        while (true) {
            switch (status) {
            case 0:
                if (doLogin()) status = 1;
                break;
            case 1:
                if (doGetList()) status = 2;
                
                
            }
        }
    }
      

  3.   

    谢谢大哥,我研究研究,另外,大哥能否提供一下您的QQ或则EMAIL,平时想和您聊聊技术问题哈,绝不打扰
      

  4.   

    以前曾经写过一个自动登录WebOnline游戏,然后挂机刷怪的程序,这个是登录成功判断的函数:
        private boolean assertLogon() throws ClientProtocolException, IOException {
            log.info("Assert logon.");
            HttpGet httpget = new HttpGet(serverConnector.getServerURL().indexPageUrl);
            HttpResponse response = serverConnector.execute(httpget);
            HttpEntity entity = response.getEntity();        if (entity != null) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), Charset
                        .forName("UTF-8")));
                try {
                    for (int i = 0; i < 15; i++) {
                        String tmp = reader.readLine();
                        if (tmp == null) {
                            break;
                        }
                        log.debug(tmp);
                        if (tmp.indexOf(assertLogonSuccess) > 0) {
                            return true;
                        }
                    }
                } finally {
                    reader.close();
                }
            }
            log.warn("Logon failed.");
            return false;
        }
    重要的其实也就三句话:
    BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), Charset.forName("UTF-8")));
    String tmp = reader.readLine();
    if (tmp.indexOf(assertLogonSuccess) > 0) { return true; }其中 String assertLogonSuccess = "登录成功,正在刷新主城";