public AccessURLJob(Collector collector) { this.collector = collector; URLDataReader reader = (URLDataReader) collector.getReader(); url = reader.getUrl(); // this.encode = reader.getEncode(); this.method = reader.getMethod(); }
@SuppressWarnings("finally") public WatchedUrl HttpTest() throws ClientProtocolException, IOException { WatchedUrl watchedUrl = new WatchedUrl(); CloseableHttpClient httpclient = HttpClients.createDefault(); CloseableHttpResponse response = null; long start = System.currentTimeMillis(); try { switch (method) { case "GET": HttpGet httpget = new HttpGet(url); response = httpclient.execute(httpget); break; case "POST": HttpPost httppost = new HttpPost(url); response = httpclient.execute(httppost); break; case "HEAD": HttpHead httphead = new HttpHead(url); response = httpclient.execute(httphead); break; } } finally { int responseTime = (int) (System.currentTimeMillis() - start); if (response != null) { watchedUrl.setStatusCode(response.getStatusLine().getStatusCode()); if (response.getStatusLine().getStatusCode() == 200 || response.getStatusLine().getStatusCode() == 302) { watchedUrl.setAvailability(true); alertServiceImpl.conutSuccess(collector); } else { watchedUrl.setAvailability(false); alertServiceImpl.countFail(collector); } } else { watchedUrl.setAvailability(false); alertServiceImpl.countFail(collector); } watchedUrl.setResponseTime(responseTime); watchedUrl.setCollectorId(collector.getId()); watchedUrl.setProductId(collector.getProductId()); watchedUrl.setMethod(method); watchedUrl.setWatchTime(new Date()); httpclient.close(); return watchedUrl; } }