只需要给它不断的发送请求就可以了,只不过1分钟内同一个用户点击多少次它都是只算一次。
基于这个,我们就想到可以用http来不断的向网页发送请求,
再写一个定时任务,1分钟执行一次。
那么项目跑起来,每分钟你的每篇博客就可以增加1个访客量了。

1.创建HttpUtil类来写一个简单的http请求方法,访问地址

package com.example.util;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;

import java.io.IOException;

/**
 * @author: 
 * @description:
 * @date: 2019-05-30 23:48
 */
public class Tool {
    public static String doGet(String url) {
        String body = "";
        HttpClient httpClient = HttpClientBuilder.create().build();
        HttpGet httpGet = new HttpGet(url);
        try{
            HttpResponse httpResponse = httpClient.execute(httpGet);
            //HttpEntity httpEntity = httpResponse.getEntity();
            //body = EntityUtits.toString(httpEntity, Consts.UTF_8);
            //释放连接
            httpGet.releaseConnection();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return body;
    }
}

2.创建定时任务Scheduled,每隔1分钟来请求一次去访问博客
可以用Spirng的@Scheduled来完成这个定时任务,我的项目是Spring boot,在启动类DemoApplication上方加上@EnableScheduling 来开启定时任务。

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling//开启定时任务
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

3.创建一个SchedulingTest类来完成这个任务

package com.example.dao;

import com.example.util.Tool;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author: 
 * @description:
 * @date: 2019-05-30 23:36
 */
@Component
public class SchedulingTest {
    private int i = 0;

    @Scheduled(fixedRate = 60 * 1000)//具体时间间隔,60*1000也就是1分钟执行一次
    void doSomethingWith() {
        String url = "https://blog.csdn.net/exodus3/article/details/90615531";
        Tool.doGet(url);
        i++;
        System.out.println("第" + i + "次访问");
    }
}

用java代码实现网站阅读量的增加_微信公众号
运行后,就会发现访问量会增加。在博客列表上刷新。可以用这些代码来增加访问网站的次数,但是有些网站是有IP或者次数的限制。这些代码供大家学习,并非鼓励大家去刷次数。

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

    

谢谢大家