Java工具类对接百度熊掌号API推送接口进行文章推送实例

目前,百度推出了百度熊掌号服务。旨在更好的为站长服务。

那么百度熊掌号如何通过推送API接口实现自动文章推送呢?

第一步:注册百度熊掌号,获取推送链接appid以及token

第二步:用java代码实现的推送,代码如下:

import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
/**
 * 
 * @from everycoding.com
 *
 */
public class BaiduUtils {
    private final static Log log = LogFactory.getLog(BaiduUtils.class);
    public static final int ORIGINAL_TYPE = 1;
    public static void doHttpRequest(String url,String postUrls) {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        httpclient.getParams().setParameter("http.protocol.cookie-policy", CookiePolicy.BROWSER_COMPATIBILITY);
        org.apache.http.params.HttpParams params = httpclient.getParams();
        org.apache.http.params.HttpConnectionParams.setConnectionTimeout(params, 5000);
        org.apache.http.params.HttpConnectionParams.setSoTimeout(params, 1000 * 60 * 10);
        DefaultHttpRequestRetryHandler dhr = new DefaultHttpRequestRetryHandler(3, true);
        HttpContext localContext = new BasicHttpContext();
        httpclient.setHttpRequestRetryHandler(dhr);
        org.apache.http.client.methods.HttpPost post = new org.apache.http.client.methods.HttpPost();
        try {
            post.setEntity(new ByteArrayEntity(postUrls.getBytes()));
            post.setURI(URI.create(url));
            HttpResponse response = null;
            response = httpclient.execute(post, localContext);
            System.out.println("Http执行结果" + EntityUtils.toString(response.getEntity(), "utf-8"));
        } catch (Exception e) {
            System.out.println("Http执行异常");
        }
    }
    
    public static void doHttpRequest(String url,List<String> urls) {
        if (urls != null && urls.size() > 0) {
            doHttpRequest(url,StringUtils.join(urls, "\n"));
        }
    }
    
    public static void post(List<String> urls,Integer type) {
        if (type != null && type == BaiduUtils.ORIGINAL_TYPE) {
            BaiduUtils.doHttpRequest("http://data.zz.baidu.com/urls?appid=你的appid&token=你的网站token&type=realtime", urls);
        }
    }
    
    public static void post(List<String> urls) {
        BaiduUtils.doHttpRequest("http://data.zz.baidu.com/urls?appid=你的appid&token=你的网站token&type=realtime", urls);
    }
    
    public static void main(String[] args) {
        List<String> arrayList = new ArrayList<String>();
        arrayList.add("http://www.everycoding.com/coding/1106.html");
        arrayList.add("http://www.everycoding.com/question/1107.html");
        BaiduUtils.post(arrayList);
    }
}


评论

*
*