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();
}
}
}
网友评论