Java模仿HTTP Get Post要求完成服裝論壇t.vhao.net主動回帖功效。本站提示廣大學習愛好者:(Java模仿HTTP Get Post要求完成服裝論壇t.vhao.net主動回帖功效)文章只能為提供參考,不一定能成為您想要的結果。以下是Java模仿HTTP Get Post要求完成服裝論壇t.vhao.net主動回帖功效正文
比來想主動發帖回帖,拿某服裝論壇t.vhao.net實驗了一下,發明可行,不外後續沒有再應用,以避免影響服裝論壇t.vhao.net正常運轉。
1、帖子鏈接的格局為
http://bbs.***.***.**/forum.php?mod=viewthread&tid=774210
最初面774210數字變更, 便可以獲得分歧的帖子
2、避免帖子揭橥會又被刪了的情形, 停止斷定帖子能否存在
3、遞增前面的 id 數字, 對每一個鏈接做回帖的 POST 要求
重難點
回帖須要用戶登錄信息
一種是應用Cookie
另外一種是停止模仿登錄
本文彩用前者
斷定 url 對應的帖子能否存在
有能夠用戶發了帖子,好比 url 為 http://bbs.***.***.**/forum.php?mod=viewthread&tid=774200
後來該帖子用戶刪除或許被治理員刪除,固然帖子不在了,然則該 tid=774200 照樣存在的
public static boolean isExist(int id) {
String tmpPath = baseRefer + id;
URL url;
try {
url = new URL(tmpPath);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.addRequestProperty("Content-Type", "text/html; charset=UTF-8");
con.addRequestProperty(
"User-Agent",
"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.104 Safari/537.36");
con.addRequestProperty("Referer", "http://t.dianping.com/register");
con.setRequestMethod("GET");
if (con.getResponseCode() == 200) {
InputStream inputStr = con.getInputStream();
String info = new String(StreamTool.read(inputStr), "UTF-8");
if (info.contains("負疚,指定的主題不存在或已被刪除或正在被審核")) {
System.out.println("id=" + id + "帖子存在或已被刪除!");
return false;
}
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
模仿發帖
代碼比擬簡略,留意事項是找到本身的Cookie,賦給String yourCookeie
用post發送一個回帖,回帖信息在 mapData.put("message", "友誼幫頂了") 中
private static final String baseRefer = "http://bbs.**.**.**/forum.php?mod=viewthread&tid=";
private static final String yourCookeie = "Q8qA_2132_saltkey=**; Q8qA_2132_lastvisit=****3699;";
public static void main(String[] args) {
int startId = 774210; // you need change
for (int i = 0; i < 100; i++) {
postMessage(startId);
startId++;
}
}
public static void postMessage(int id) {
if (!isExist(id)) {
return;
}
String tmpPath = baseRefer + id;
StringBuilder path = new StringBuilder(tmpPath);
Map<String, String> mapData = new LinkedHashMap<String, String>();
mapData.put("mod", "post");
mapData.put("action", "reply");
mapData.put("replysubmit", "yes");
mapData.put("infloat", "yes");
mapData.put("handlekey", "fastpost");
mapData.put("inajax", "1");
mapData.put("message", "友誼幫頂了");
mapData.put("formhash", "86ec5d81");
try {
for (Map.Entry<String, String> mapEnt : mapData.entrySet()) {
path.append("&");
path.append(mapEnt.getKey() + "=");
path.append(URLEncoder.encode(mapEnt.getValue(), "UTF-8"));
}
URL url = new URL(path.toString());
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
con.setRequestProperty("Content-Length",
String.valueOf(path.length()));
con.setRequestProperty(
"User-Agent",
"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.104 Safari/537.36");
con.setRequestProperty("Cookie", yourCookeie);
con.setDoOutput(true);
OutputStream outStr = con.getOutputStream();
outStr.write(path.toString().getBytes());
if (con.getResponseCode() == 200) {
InputStream inputStr = con.getInputStream();
String info = new String(StreamTool.read(inputStr), "UTF-8");
System.out.println("在id=" + id + "勝利發帖!");
try {
Thread.sleep(20 * 1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
還有一個對象辦法,將輸出流轉化為字節
class StreamTool {
public static byte[] read(InputStream inputStr) throws Exception {
ByteArrayOutputStream outStr = new ByteArrayOutputStream();
// TODO Auto-generated method stub
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inputStr.read(buffer)) != -1) {
outStr.write(buffer, 0, len);
}
inputStr.close();
return outStr.toByteArray();
}
}
後果圖:
以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐。