自动提交站点最新文章到百度

如果个人拥有自己的站点,并且想把最近更新的文件提交到百度,参考  http://zhanzhang.baidu.com/linksubmit/index 。

官方没有给出java 示例,这里写了一个,测试通过。


import java.io.IOException;
import java.util.List;


import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;



public
static String pushUrl(String url) { int timeout = 30; RequestConfig config = RequestConfig.custom() .setConnectTimeout(timeout * 1000) .setConnectionRequestTimeout(timeout * 1000) .setSocketTimeout(timeout * 1000).build(); CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(config).build(); HttpPost hp = new HttpPost("接口调用地址, 见: http://zhanzhang.baidu.com/linksubmit/index"); hp.setHeader("Host", "data.zz.baidu.com"); hp.setHeader("User-Agent", "curl/7.12.1"); hp.setHeader("Content-Type", "text/plain"); HttpResponse httpresponse; HttpEntity entity = null; try { StringEntity jsonEntity = new StringEntity(url, ContentType.TEXT_PLAIN); hp.setEntity(jsonEntity); httpresponse = httpClient.execute(hp); entity = httpresponse.getEntity(); String body = EntityUtils.toString(entity); return body; } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if(entity != null) try { entity.getContent().close(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } hp.releaseConnection(); } return null; }

1、 用到 httpclient 包。apache 站点可下载。

2、 StringEntity jsonEntity = new StringEntity(url, ContentType.TEXT_PLAIN);  

      hp.setEntity(jsonEntity);

     实际是 http post Json 提交方法

 

原文地址:https://www.cnblogs.com/heyus/p/4618846.html