(1)关键源码
使用HttpClient, 来访问某一个网址。关键函数: httpclient.execute() 这个就是执行访问
/** * 发送 get请求 * @param url 访问网址的url: eg: https://www.baidu.com */ public static void get(String url) { CloseableHttpClient httpclient = HttpClients.createDefault(); // 创建httpget. HttpGet httpget = new HttpGet(url); // 执行get请求. try { CloseableHttpResponse response = httpclient.execute(httpget); HttpEntity httpEntity = response.getEntity(); System.out.println(EntityUtils.toString(httpEntity)); response.close(); } catch (IOException e) { e.printStackTrace(); } }
这个使用到了线程的睡眠。设置一个死循环,让程序一直运行下去。再使用一个线程,让程序第隔一段再重新跑动。
public static void main(String[] args) throws InterruptedException { while (true) { String url = "https://blog.csdn.net/weixin_39454194/article/details/79560111"; HttpClient.get(url); System.out.println("/**\n" + "*\n" + "*\n" + "*\n" + "*\n" + "*\n" + "*\n" + "正在刷csdn阅读量;每10分钟访问一次" + "*\n" + "*\n" + "*\n" + "*\n" + "*\n" + "*\n" + "*\n" + "**/"); //每10分钟访问一次 Thread.currentThread().sleep(1000 * 60 * 10); }
(2)打包
我这里使用IDEA来打包,
File=>Project Structure=>Artifacts=> + => JAR => 编写信息 =》 OK
Bulid => Bulid Artifacts> Bulid
即可编译可运行jar包
(3)放进服务器
把上步骤编译好的jar包放到配置好java环境的,都可以使用命令
java -jar csdn.jar
来运行!
(4)效果
参考链接:
(1)https://blog.csdn.net/xuemengrui12/article/details/74984731
(2)https://blog.csdn.net/wangpeng047/article/details/19624529