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

编程入门 行业动态 更新时间:2024-10-21 17:41:17

使用定时任务向百度推送<a href=https://www.elefans.com/category/jswz/34/1771113.html style=网站URL"/>

使用定时任务向百度推送网站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是登录百度站长之后生成的tokenpublic String pushPost(String PostUrl,String domain, String bdToken){String linkSubmitUrl="";String host="data.zz.baidu.com";linkSubmitUrl+="?site="+domain+"&token="+bdToken;String result="";HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();//HttpClientCloseableHttpClient 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

更多推荐

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

本文发布于:2023-07-01 07:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/972767.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:网站   URL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!