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


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

     

  • 相关阅读:
    搜狗拼音输入法候选框有时会跑到屏幕的左上角
    力扣 94. 二叉树的中序遍历
    让我们了解 Ceph 分布式存储
    HDFS的架构和设计要点
    业级PPTP服务器搭建企
    FastCGI中fastcgi_param 详细说明
    LAMP两种模式
    CentOS 7.4搭建LAMP,LAMP:Linux、Apache、MySQL、PHP
    centos7 7.3php编译安装
    Linux下编译安装MariaDB
  • 原文地址:https://www.cnblogs.com/heyus/p/4618846.html
Copyright © 2020-2023  润新知