个人站长百度蜘蛛有推送限制,我的个人帐号一天只能推送10条,这也太小了,不过还是用一个这个api吧,操作起来也不难,效果如下:
点击蜘蛛推送,调用api,具体java代码如下:
else if(p0!=null&&p0.equals("bdPush")) {
String ids = request.getParameter("ids");
if(ids!=null) {
String id[] = ids.split("\\,");
boolean flag = false;
for(int i=0;i<id.length;i++) {
String result = BaiduPostUtils.sendPost("https://www.mbzj.net/article/view/"+id[i]);
System.out.println("蜘蛛推送返回:"+result);
if(StringUtils.isNotEmpty(result)) {
JSONObject json = JSON.parseObject(result);
if(json.containsKey("remain")&&json.getIntValue("remain")==0) {
flag = true;
break;
}
}else {
jsobject.put("code", -1);
jsobject.put("msg", "百度API推送额度已用完,明天再试");
}
}
if(flag) {
jsobject.put("code", 1);
jsobject.put("msg", "百度API推送额度已用完,明天再试");
}else {
jsobject.put("code", 0);
jsobject.put("msg", "批量推送成功");
}
}
}
上面的代码主要调用了BaiduPostUtils,其中token要换成你自己申请的token 具体如下:
public static String sendPost( String postData) {
try {
URL url = new URL("http://data.zz.baidu.com/urls?site=www.mbzj.net&token=xxx");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "text/plain; charset=UTF-8");
connection.setDoOutput(true);
connection.setUseCaches(false);
try (DataOutputStream wr = new DataOutputStream(connection.getOutputStream())) {
wr.writeBytes(postData);
wr.flush();
wr.close();
}
int responseCode = connection.getResponseCode();
StringBuilder response = new StringBuilder();
if (responseCode == HttpURLConnection.HTTP_OK) {
try (BufferedReader in = new BufferedReader(
new InputStreamReader(connection.getInputStream()))) {
String inputLine;
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
}
} else {
throw new RuntimeException("Failed : HTTP error code : " + responseCode);
}
return response.toString();
}catch(Exception e) {
e.printStackTrace();
}
return null;
}
也不清楚为啥,对个人站点每天只有10次的限制,这个也太不合理了。不过没办法,谁叫搜索百度一家独大呢,或许伙伴们有没有更好的办法?
推送效果查看https://www.mbzj.net
百度收录还是很及时的