zoukankan      html  css  js  c++  java
  • 自动提交站点最新文章到百度

    如果个人拥有自己的站点,并且想把最近更新的文件提交到百度,参考  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 提交方法

     

  • 相关阅读:
    差分约束+SPFA+栈
    差分约束问题讲解博客
    最小费用最大流2
    最小费用最大流
    合并油田
    PHP核心技术与最佳实践--笔记
    PHP命令行模式
    vim一些常用的快捷键
    varnish 的一个配置
    redis在我工作中的实际应用
  • 原文地址:https://www.cnblogs.com/heyus/p/4618846.html
Copyright © 2011-2022 走看看