Apache HttpClient 4.3开发指南
- - CSDN博客推荐文章《Apache HttpClient 4.3开发指南》. 作者:chszs,转载需注明. 博客主页: http://blog.csdn.net/chszs. Apache HttpClient 4系列已经发布很久了,但由于它与HttpClient 3.x版本完全不兼容,以至于业内采用此库的公司较少,在互联网上也少有相关的文档资料分享.
SSLContext sslcontext = SSLContext.getInstance("TLS"); sslcontext.init(null, new TrustManager[]{easyTrustManager}, null); SSLSocketFactory sf = new SSLSocketFactory(sslcontext); Scheme sch = new Scheme("https", sf,443); httpclient.getConnectionManager().getSchemeRegistry().register(sch);
httpclient.getParams().setIntParameter("http.socket.timeout",30000);//毫秒
public void run(){ try { System.out.println("-----设置代理服务器----"); String proxyFileString0 = FileUtils.readFileToString(new File(ApplicationUtils.getWebrootDir()+"/order/proxy_add.txt"), "UTF-8"); String proxyFileString1 = FileUtils.readFileToString(new File(ApplicationUtils.getWebrootDir()+"/order/proxy_enable.txt"), "UTF-8"); String proxyFileString = proxyFileString0+"\n"+proxyFileString1; for(String proxyString : StringUtils.split(proxyFileString, "\n")){ proxyString = proxyString.trim(); if(StringUtils.isNotEmpty(proxyString)){ System.out.print("-----测试代理服务器: "+proxyString); String[] proxyInfo = proxyString.split(":"); HttpHost httpHost = new HttpHost(proxyInfo[0], Integer.valueOf(proxyInfo[1])); long start = System.currentTimeMillis(); if(HttpUtils.testProxy(httpHost)){ long usetime = System.currentTimeMillis()-start; if(usetime > 10000){ System.out.println(" 不使用,响应时间太长时间:"+usetime+"毫秒----"); }else{ System.out.println(" 可使用,使用时间:"+usetime+"毫秒----"); httpHostList.add(httpHost); } } } } System.out.println("-----设置代理服务器成功,总数:"+httpHostList.size()+"----"); } catch (Exception e) { } }
//设置代理对象 ip/代理名称,端口 try{ httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,ProxyHostUtils.next()); }catch(Exception e){}
/** * 读取登录验证码 * @throws Exception */ @Transient public void getRemoteLoginCode() throws Exception { File file = HttpUtils.doGetFile(OrderRunTask.loginCodeUrl+"&nocache="+Utils.getRandomString(10), cookieContext); if(file != null){ File codeFile = new File(ApplicationUtils.getWebrootDir()+getLoginCodeUrl()); FileUtils.copyFile(file, codeFile); } } /** * 读取提交订单验证码 * @throws Exception */ @Transient public void getRemoteSumitCode() throws Exception { File file = HttpUtils.doGetFile(OrderRunTask.submitCodeUrl+"&nocache="+Utils.getRandomString(10), cookieContext); if(file != null){ File codeFile = new File(ApplicationUtils.getWebrootDir()+getSubmitCodeUrl()); FileUtils.copyFile(file, codeFile); } }
//建立线程池 public static ExecutorService runningTaskPool = Executors.newFixedThreadPool(1000); //启动订票任务 OrderRunTask orderRunTask = new OrderRunTask(account, orderTask); OrderRunTaskUtils.runningTaskMap.put(orderTask.getId(), orderRunTask); Future<?> future = OrderRunTaskUtils.runningTaskPool.submit(orderRunTask); orderRunTask.setFuture(future); //中断任务 OrderRunTask orderRunTask = OrderRunTaskUtils.runningTaskMap.get(taskId); try{ orderRunTask.setStop(); orderRunTask.getFuture().cancel(true); }catch(Exception e){ e.printStackTrace(); } //判断是否订票成功,并且发送邮件 if(body.indexOf("45分钟") != -1){ Date now = new Date(); account.sendMail(orderTask.getQueryTrainDate()+"_订票成功", orderTask.getQueryTrainDate() +" "+ orderTask.getFromStation()+"到"+orderTask.getToStation()+(StringUtils.isEmpty(orderTask.getTrainNo())?orderTask.getTrainNo():"")+" --订票时间:" +DateUtils.getDateTime("HH:mm:ss", now) ); account.setReload(true); orderTask.setState(200); orderTask.setRuningTime(now); this.account.error(orderTask.toString()+": 订票成功。"); break next; }