记 刷csdn博客的一次折腾

(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

        使用Intellij Idea打包java为可执行jar包

(2)https://blog.csdn.net/wangpeng047/article/details/19624529

        

HttpClient使用详解


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值