使用定时任务向百度推送网站URL

在springboot里使用task定时任务特别容易,简单的说就启动类添加一个enableXXX注解,然后再需要的方法上添加Scheduledz注解,注解里cron表达式网上百度一下一大把。

启动类添加注解
@SpringBootApplication
@EnableScheduling
public class TaskApplication {
    public static void main(String[] args) {
        SpringApplication.run(TaskApplication.class,args);
    }
}
方法上使用 @Scheduled 注解
@Scheduled(cron = "0 0 0 1/1 * ? ")  //这代表每天的凌晨12点开始执行定时方法

通过以上操作定时器的任务就可以正常运行了,但是要通过定时去向百度推送数据,不会像怎么简单了,直接看代码吧

//postUrl是需要推送的url链接,domain是你的站点url链接,token是登录百度站长之后生成的token
 public String pushPost(String PostUrl,String domain, String bdToken){
        String linkSubmitUrl="http://data.zz.baidu.com/urls";
        String host="data.zz.baidu.com";
        linkSubmitUrl+="?site="+domain+"&token="+bdToken;
        String result="";
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        //HttpClient
        CloseableHttpClient client = httpClientBuilder.build();
        client = (CloseableHttpClient) wrapClient(client);
        Map<String, String> msg=new HashMap<>();
        HttpPost post = new HttpPost(linkSubmitUrl);
        //发送请求参数
        try
        {
            StringEntity s = new StringEntity(PostUrl,"utf-8");
            s.setContentType("application/json");
            post.setEntity(s);
            post.setHeader("Host", host);
            post.setHeader("User-Agent", "curl/7.12.1");
            post.setHeader("Content-Type", "text/plain");
            HttpResponse res = client.execute(post);
            HttpEntity entity = res.getEntity();
            String str= EntityUtils.toString(entity, "utf-8");
            result=str;

        }
        catch (Exception e)
        {
            result=null;
            e.printStackTrace();
        }
        return result;
    }

    private static HttpClient wrapClient(HttpClient client) {
        try {
            SSLContext ctx = SSLContext.getInstance("TLSv1");
            X509TrustManager tm = new X509TrustManager() {
                public void checkClientTrusted(X509Certificate[] xcs,
                                               String string) throws CertificateException {
                }
                public void checkServerTrusted(X509Certificate[] xcs,
                                               String string) throws CertificateException {
                }
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
            };
            ctx.init(null, new TrustManager[] { tm }, null);
            SSLConnectionSocketFactory sslSf = new SSLConnectionSocketFactory(ctx,
                    new String[] {"TLSv1"}, null,new DefaultHostnameVerifier());
            CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslSf).build();
            return httpclient;

        } catch (Exception ex) {
            return null;
        }
    }

把需要导的包也放出来吧,万一导错了就尴尬了

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.DefaultHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

好了,到这里就可以对你的网站进行百度推送了,慢慢等待被百度收录就可以了
需要注意的是在进行定时任务的时候需要留意一下是否成功推送到百度,是否成功可以看返回的状态码,若成功的话就会返回当天剩余可推送的次数,若失败则返回状态码以及错误提示信息!!!

博客地址:徐启君的个人博客 xqijun.top

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值