1.pom.xml
1.7.21 3.4.2 4.5.2 1.1.7 org.slf4j slf4j-api ${slf4j.version} org.slf4j slf4j-ext 1.7.21 ch.qos.logback logback-classic ${logback.version} org.apache.httpcomponents httpclient ${httpclient.version} com.squareup.okhttp3 okhttp ${okhttp.version}
2. 代码
public void sendGet(String url){ CloseableHttpClient httpclient = HttpClients.createDefault(); RequestConfig requestConfig = RequestConfig .custom() .setSocketTimeout(1000*60*60*5) .setConnectTimeout(1000*60*60*5) .build(); HttpGet httpget = new HttpGet(url); httpget.setConfig(requestConfig); try { CloseableHttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); logger.info("response is {}" ,response); logger.info("entity is {}" ,entity); logger.info("result is {}" ,EntityUtils.toString(entity)); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { //response.close(); } }
3.测试
public static void main(String[] args) throws InterruptedException { Sample sample=new Sample(); Calendar start=Calendar.getInstance(); start.set(2017, 7, 23); Calendar end=Calendar.getInstance(); end.set(2017, 7, 24); SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd"); while(true){ String startDate=formatter.format(start.getTime()); start.add(Calendar.DAY_OF_MONTH, 1); String endDate=formatter.format(start.getTime()); String url="http://test.test.com/daily/getDailyAction?startDate="+startDate +"&endDate="+endDate; if(start.after(end)){ break; }else{ sample.sendGet(url); Thread.sleep(1000*60*2); } } }