日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java调用百度推送详解_Java 以 Post 方式实现百度 Sitemap 实时推送

發(fā)布時間:2023/12/19 java 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java调用百度推送详解_Java 以 Post 方式实现百度 Sitemap 实时推送 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Sitemap 可方便網(wǎng)站管理員通知搜索引擎他們網(wǎng)站上有哪些可供抓取的網(wǎng)頁。最簡單的 Sitemap 形式,就是XML 文件,在其中列出網(wǎng)站中的網(wǎng)址以及關于每個網(wǎng)址的其他元數(shù)據(jù)(上次更新的時間、更改的頻率以及相對于網(wǎng)站上其他網(wǎng)址的重要程度為何等),以便搜索引擎可以更加智能地抓取網(wǎng)站。

百度提供了多種鏈接提交方式如下:

1、主動推送:最為快速的提交方式,推薦您將站點當天新產(chǎn)出鏈接立即通過此方式推送給百度,以保證新鏈接可以及時被百度收錄。

2、sitemap:您可以定期將網(wǎng)站鏈接放到sitemap中,然后將sitemap提交給百度。百度會周期性的抓取檢查您提交的sitemap,對其中的鏈接進行處理,但收錄速度慢于主動推送。

3、手工提交:一次性提交鏈接給百度,可以使用此種方式。

post 推送示例:

POST /urls?site=www.yoodb.com&token=wkkpNynvtltDW347 HTTP/1.1

User-Agent: curl/7.12.1

Host: data.zz.baidu.com

Content-Type: text/plain

Content-Length: 83

http://www.example.com/1.html

http://www.example.com/2.html

下面為大家講述一下 Java 以 Post 方式實現(xiàn)百度 Sitemap 實時推送,具體代碼如下:

package com.yoodb.util;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import java.net.URL;

import java.net.URLConnection;

import java.util.Arrays;

import java.util.List;

import java.util.Map;

import com.google.gson.Gson;

public class PushSiteMapUtil {

/**

* 推送 Stiemap 到百度

* @param url

* @return

*/

@SuppressWarnings("rawtypes")

public static boolean push(String url){

String urlPath = "http://data.zz.baidu.com/urls?site=www.yoodb.com&token=xreflkjfasD";

try {

String re = postHttp(urlPath, Arrays.asList(url));

System.out.println(re);

Map map = new Gson().fromJson(re, Map.class);

if(map.get("success") !=null && !map.get("success").equals("")){

return true;

}

} catch (Exception e) {

return false;

}

return false;

}

/**

* 百度鏈接實時推送

* @param postUrl

* @param params

* @return

*/

private static String postHttp(String url,List params){

if(null == url || null == params || params.isEmpty()){

return null;

}

String result = "";

PrintWriter out = null;

BufferedReader in = null;

try {

URLConnection conn=new URL(url).openConnection();// 建立URL之間的連接

conn.setRequestProperty("Host","data.zz.baidu.com");// 設置通用的請求屬性

conn.setRequestProperty("User-Agent", "curl/7.12.1");

conn.setRequestProperty("Content-Length", "83");

conn.setRequestProperty("Content-Type", "text/plain");

conn.setDoInput(true);// 發(fā)送POST請求必須設置如下兩行

conn.setDoOutput(true);

out = new PrintWriter(conn.getOutputStream());// 獲取conn對應的輸出流

String param = "";// 發(fā)送請求參數(shù)

for(String s : params){

param += s + "\n";

}

out.print(param.trim());

out.flush();

in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

String line;

while((line=in.readLine())!= null){

result += line;

}

} catch (Exception e) {

e.printStackTrace();

} finally{

try{

if(out != null)

out.close();

if(in!= null)

in.close();

}catch(IOException ex){

ex.printStackTrace();

}

}

return result;

}

}

測試代碼如下:

public static void main(String[] args) {

boolean re = PushSiteMapUtil.push("http://www.yoodb.com/article/display/1074");

System.out.println(re);

}

輸出結果如下:

{"remain":493,"success":1}

true

推送成功

狀態(tài)碼為200,可能返回以下字段:

字段

是否必選

參數(shù)類型

說明

success

int

成功推送的url條數(shù)

remain

int

當天剩余的可推送url條數(shù)

not_same_site

array

由于不是本站url而未處理的url列表

not_valid

array

不合法的url列表

成功返回示例:

{

"remain":4999998,

"success":2,

"not_same_site":[],

"not_valid":[]

}

推送失敗

狀態(tài)碼為4xx,返回字段有:

字段

是否必傳

類型

說明

error

int

錯誤碼,與狀態(tài)碼相同

message

string

錯誤描述

失敗返回示例:

{

"error":401,

"message":"token is not valid"

}

總結

以上是生活随笔為你收集整理的java调用百度推送详解_Java 以 Post 方式实现百度 Sitemap 实时推送的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。