import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.springframework.scheduling.annotation.Scheduled;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
/**
*@author will
*/
public class PostToXiongZhangHao{
// 每12小时提交一次
@Scheduled(cron = "0 0 0/12 * * ?")
public void postToXiongZhangHao() {
// 在此之前根据业务获取List并复制给list
List<String> list = new ArrayList<>();
String path = "/Users/will/Downloads/xiongzhanghao.txt";
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost postMethod = new HttpPost("http://data.zz.baidu.com/urls?appid=yourAppid&token=yourToken&type=batch");
postMethod.setHeader("Content-Type", "text/plain");
postMethod.setHeader("User-Agen", "curl/7.12.1");
StringEntity entity = new StringEntity(list.toString(), "UTF-8");
postMethod.setEntity(entity);
// 写入txt文件,以备查看和curl使用
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path, true)));
for (String string : list) {
writer.write(string + "\r\n");
}
writer.close();
HttpResponse response = httpClient.execute(postMethod);
int code = response.getStatusLine().getStatusCode();
if (!"200".equals(String.valueOf(code))) {
System.err.println("提交失败");
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Java 百度 熊掌号 资源提交 API提交
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...