java 推送链接到百度资源搜索平台
406人浏览 / 0人评论
百度资源搜索平台:https://ziyuan.baidu.com/dashboard
代码:
import com.google.common.collect.Lists;
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;
/**
* @Author FengHao
* @Date 2023/3/2
* @Description: 推送文章到百度搜索
**/
public class BaiduPushUtils {
public static void main(String[] args) {
List<String> parameters = Lists.newArrayList("https://www.jbritian.com/article/61");
String resp = pushUrl(parameters);
System.err.println(resp);
}
public static String pushUrl(String[] urls) {
return pushUrl(Arrays.asList(urls));
}
/**
* 百度链接实时推送
*/
public static String pushUrl(List<String> parameters) {
String url = "http://data.zz.baidu.com/urls?site=https://www.jbritian.com&token=aPGHofTZRw8iILsL";
if (null == parameters || parameters.size() == 0) {
return null;
}
StringBuilder result = new StringBuilder();
PrintWriter out = null;
BufferedReader in = null;
try {
//建立URL之间的连接
URLConnection conn = new URL(url).openConnection();
//设置通用的请求属性
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");
//发送POST请求必须设置如下两行
conn.setDoInput(true);
conn.setDoOutput(true);
//获取conn对应的输出流
out = new PrintWriter(conn.getOutputStream());
//发送请求参数
StringBuilder param = new StringBuilder();
for (String s : parameters) {
param.append(s).append("\n");
}
out.print(param.toString().trim());
//进行输出流的缓冲
out.flush();
//通过BufferedReader输入流来读取Url的响应
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result.append(line);
}
} catch (Exception e) {
System.out.println("发送post请求出现异常!" + e);
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result.toString();
}
}
全部评论