java调用百度蜘蛛api,及时推送最新内容

个人站长百度蜘蛛有推送限制,我的个人帐号一天只能推送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

百度收录还是很及时的

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值