美文网首页
HttpPost调用百度收录API

HttpPost调用百度收录API

作者: AC编程 | 来源:发表于2022-04-27 11:41 被阅读0次

    一、代码

    import cn.hutool.core.collection.CollectionUtil;
    import com.alibaba.fastjson.JSONObject;
    import com.alanchen.qm.common.core.utils.StringUtil;
    import lombok.extern.slf4j.Slf4j;
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClientBuilder;
    import org.apache.http.util.EntityUtils;
    import org.springframework.stereotype.Component;
    
    import java.util.List;
    
    /**
     * @author Alan Chen
     * @description 百度收录
     * @date 2022/4/26
     */
    @Slf4j
    @Component
    public class BaiDuIncludeComponent {
    
        private static final String DYNAMIC_URL = "https://xxxx.com/dynamic/";
    
        private static final String LINK_SUBMIT_URL = "http://data.zz.baidu.com/urls?site=https://xxxx.com&token=xxxxxxx";
    
        private static final String API_HOST = "data.zz.baidu.com";
    
        /**
         * 单个提交
         *
         * @param dynamicId
         * @return
         */
        public boolean dynamicInclude(String dynamicId) {
            log.info("百度收录,dynamicId={}", dynamicId);
            String postUrl = DYNAMIC_URL + dynamicId;
            return pushPost(postUrl);
        }
    
        /**
         * 批量提交
         *
         * @param dynamicIdList
         * @return
         */
        public boolean dynamicInclude(List<String> dynamicIdList) {
            if (CollectionUtil.isEmpty(dynamicIdList)) {
                return false;
            }
    
            StringBuilder builder = new StringBuilder();
            for (String id : dynamicIdList) {
                builder.append(DYNAMIC_URL + id).append("\n");
            }
            String postUrl = builder.toString();
            return pushPost(postUrl);
        }
    
        /**
         * 百度链接实时推送
         *
         * @param postUrl 需要推送的内容链接
         * @return
         */
        public boolean pushPost(String postUrl) {
            HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
            CloseableHttpClient client = httpClientBuilder.build();
    
            HttpPost post = new HttpPost(LINK_SUBMIT_URL);
            //发送请求参数
            try {
                StringEntity s = new StringEntity(postUrl, "utf-8");
                s.setContentType("application/json");
                post.setEntity(s);
                post.setHeader("Host", API_HOST);
                post.setHeader("User-Agent", "curl/7.12.1");
                post.setHeader("Content-Type", "text/plain");
                HttpResponse res = client.execute(post);
    
                try {
                    HttpEntity entity = res.getEntity();
                    String result = EntityUtils.toString(entity, "utf-8");
                    log.info("result={}", result);
                    if (StringUtil.isNotBlank(result)) {
                        Object error = JSONObject.parseObject(result).get("error");
                        return error == null;
                    }
                } catch (Exception e) {
                    log.error("结果解析出错,e={}", e.getMessage());
                    return false;
                }
                return false;
            } catch (Exception e) {
                log.error("收录出错,e={}", e.getMessage());
                return false;
            }
        }
    }
    

    二、结果

    2.1 成功结果
    {"remain":2997,"success":2}
    

    说明:

    • success:成功条数
    • remain:当日可收录剩余条数,每个站点配额都不一样,新站点一般配额是每日3000条。
    2.2 失败结果
    {"remain":3000,"success":0,"not_same_site":["http://xxxxx.com/dynamic/625fd2e74a353a1b8612ab13"]}
    
    • not_same_site:站点域名和提交收录的域名要一致。

    相关文章

      网友评论

          本文标题:HttpPost调用百度收录API

          本文链接:https://www.haomeiwen.com/subject/ahnkyrtx.html