JAVA对接百度API提交普通收录/SEO
说明:
为了增加内容的曝光量,除了主动让百度爬虫爬取之外,还有普通收录的API提交,程序主动向百度搜索推送资源,缩短爬虫发现网站链接的时间。
官网地址:http://data.zz.baidu.com/
百度推广API的token获取地址:
http://data.zz.baidu.com/site/index
进入之后点击 添加网站
填写完上面这些之后会进行验证, 验证完之后,选择链接提交,有个自动提交能看到密钥;
另外,其实当你完成添加网站之后,来到普通收录界面,API提交是可以直接看到你的token的,如下:
demo:
JAVA对接的demo完整版,可直接拿来用:
package com.test;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 javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.Map;public class Test2021117 {/*** 百度链接实时推送* @param postUrl 需要推送的内容链接* @return*/public String pushPost(String postUrl) {//推送链接的域名,即 你的网站的域名String domain = "https://www.xxxxx.com";//百度主动推送链接准入密钥,即获取到的tokenString bdToken = "xxxxxxxxxxxxxxxxx";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();//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 org.apache.http.client.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;}}
}
总结:
如果返回的 result 中包含 success 字段 即可说明提交成功,次日也可在百度的平台–站点信息 里面看到你主动提交的次数: