importorg.apache.http.HttpResponse;importorg.apache.http.NameValuePair;importorg.apache.http.client.HttpClient;importorg.apache.http.client.entity.UrlEncodedFormEntity;importorg.apache.http.client.methods.HttpPost;importorg.apache.http.message.BasicNameValuePair;importorg.jsoup.Jsoup;importorg.jsoup.nodes.Document;importorg.jsoup.nodes.Element;importorg.jsoup.select.Elements;importjava.io.BufferedReader;importjava.io.File;importjava.io.FileReader;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.Iterator;importjava.util.List;importjava.util.Map;public classWeiboTest {public static final String url = "https://weibo.com/aj/mblog/del?ajwvr=6";public static final String charset = "utf-8";public static void main(String[] args) throwsException {/*** sina_weibo.txt由微博页面F12获得格式是
*
*
*
*
*
*
* ...
*
BufferedReader br= new BufferedReader(newFileReader(file));
StringBuffer sb= newStringBuffer();
String str;while ((str = br.readLine()) != null) {
sb.append(str).append("\n");
}
System.out.println(sb);
br.close();
Document doc=Jsoup.parseBodyFragment(sb.toString());
Element body=doc.body();
Elements elements= body.select("div[action-type='feed_list_item']");
Iterator it =elements.iterator();
HttpPost httpPost= newHttpPost(url);
addHeader(httpPost);while(it.hasNext()) {
Element element=it.next();
String mid= element.attr("mid");
System.out.println(mid);
execute(httpPost, mid);
}
}private static void execute(HttpPost httpPost, String mid) throwsException {
HttpClient httpClient= newSSLClient();//设置参数
List list = new ArrayList();
Map map = new HashMap();
map.put("mid", mid);
Iterator> iterator =map.entrySet().iterator();while(iterator.hasNext()) {
Map.Entry elem =iterator.next();
list.add(newBasicNameValuePair(elem.getKey(), elem.getValue()));
}if (list.size() > 0) {
UrlEncodedFormEntity entity= newUrlEncodedFormEntity(list, charset);
httpPost.setEntity(entity);
}
HttpResponse resp=httpClient.execute(httpPost);
System.out.println(resp);
}private static voidaddHeader(HttpPost httpPost) {
httpPost.addHeader("Accept", "*/*");
httpPost.addHeader("Accept-Encoding", "gzip, deflate, br");
httpPost.addHeader("Accept-Language", "zh-CN,zh;q=0.9");
httpPost.addHeader("Connection", "keep-alive");
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");/*** 设置自己的cookie*/httpPost.addHeader("Cookie", "***");
httpPost.addHeader("Host", "weibo.com");
httpPost.addHeader("Origin", "https://weibo.com");
httpPost.addHeader("Referer", "https://weibo.com/1864722372/profile?rank=1&is_all=1");
httpPost.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.62 Safari/537.36");
httpPost.addHeader("X-Requested-With", "XMLHttpRequest");
}
}