Java類比HTTP Get Post請求實現論壇自動回帖功能_java

來源:互聯網
上載者:User

最近想自動發帖回帖,拿某論壇實驗了一下,發現可行,不過後續沒有再使用,以免影響論壇正常運行。

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(); }}

效果圖:

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.