`

JAVA 编写代码自动在 discuz 7.2 的论坛上发帖子

    博客分类:
  • java
阅读更多

遇到一个 Powered by discuz 7.2 ! 的论坛,有积分的限制,很是不爽,发帖可以增加积分,所以花了点时间琢磨弄个自动发帖机,以下是基本的代码和原理说明,要实现自动发帖,循环执行以下代码即可。(注意发帖的时间间隔限制)

 

Java代码  收藏代码
  1. package com.ldq;  
  2.   
  3. import java.io.InputStream;  
  4. import java.io.OutputStreamWriter;  
  5. import java.net.HttpURLConnection;  
  6. import java.net.URL;  
  7. import java.util.List;  
  8. import java.util.Map;  
  9.   
  10. public class MyNewTopic {  
  11.   
  12.     private static URL url;  
  13.     private static HttpURLConnection con;  
  14.     private static String temp;  
  15.     private static InputStream is;  
  16.     private static byte[] b;  
  17.     private static int pos;  
  18.     private static String cookie_sid;  
  19.     private static String cookie_auth;  
  20.     private static String my_cookie;  
  21.     private static String login_formhash;  
  22.     private static String post_formhash;  
  23.     private static String user = "test";// 用户名  
  24.     private static String pass = "test";// 密码  
  25.     private static String new_fid = "11";// 版块 ID  
  26.     private static String subject = "新主题";// 标题  
  27.     private static String msg = "这里是新主题的内容";// 帖子内容  
  28.   
  29.     public static void main(String[] args) {  
  30.         // TODO Auto-generated method stub  
  31.         try {  
  32.   
  33.             // 获取 cookie_sid 和 login_formhash --------------------  
  34.             url = new URL("http://192.168.72.130/bbs/logging.php?action=login");  
  35.             con = (HttpURLConnection) url.openConnection();  
  36.             if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {  
  37.                 // 获取服务器发给客户端的 Cookie  
  38.                 temp = con.getHeaderField("Set-Cookie");  
  39.                 System.out.println("Set-Cookie:" + temp);  
  40.                 // 取 Cookie 前面的部分就可以了,后面是过期时间、路径等,不用管它  
  41.                 cookie_sid = temp.substring(014);  
  42.                 System.out.println(cookie_sid);  
  43.   
  44.                 is = con.getInputStream();  
  45.                 b = new byte[is.available()];  
  46.                 is.read(b);  
  47.                 // 服务器会返回一个页面,此页面中包含 formhash  
  48.                 temp = new String(b);  
  49.                 // 找出这个 formhash 的位置  
  50.                 pos = temp.indexOf("name=\"formhash\" value=");  
  51.                 // System.out.println(temp);  
  52.                 // 找出这个 formhash 的内容,这是登录用的 formhash  
  53.                 login_formhash = temp.substring(pos + 23, pos + 23 + 8);  
  54.                 System.out.println("login_formhash:" + login_formhash);  
  55.                 System.out  
  56.                         .println("------------------------------------------------------------");  
  57.                 is.close();  
  58.             }  
  59.   
  60.             // 获取cookie_auth -----------------------------------------------  
  61.             url = new URL("http://192.168.72.130/bbs/logging.php");  
  62.             con = (HttpURLConnection) url.openConnection();  
  63.   
  64.             // 设定以 POST 发送  
  65.             con.setRequestMethod("POST");  
  66.             // 加入 Cookie 内容  
  67.             con.setRequestProperty("Cookie", cookie_sid);  
  68.             // 添加 POST 的内容  
  69.             con.setDoOutput(true);  
  70.             OutputStreamWriter osw = new OutputStreamWriter(con  
  71.                     .getOutputStream());  
  72.             osw  
  73.                     .write("action=login&loginfield=username&questionid=0&answer=&loginsubmit=yes&formhash="  
  74.                             + login_formhash  
  75.                             + "&username="  
  76.                             + user  
  77.                             + "&password=" + pass);  
  78.             osw.flush();  
  79.             osw.close();  
  80.   
  81.             if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {  
  82.                 Map<String, List<String>> map = con.getHeaderFields();  
  83.                 List<String> list = map.get("Set-Cookie");  
  84.                 for (int i = 0; i < list.size(); i++) {  
  85.                     temp = list.get(i);  
  86.                     if (temp.contains("CsN_auth")) {  
  87.                         System.out.println(temp);  
  88.                         // 取 Cookie 前面的部分就可以了,后面是过期时间、路径等,不用管它  
  89.                         cookie_auth = temp.split(";")[0];  
  90.                         System.out.println("cookie_auth:" + cookie_auth);  
  91.                     }  
  92.                 }  
  93.   
  94.                 is = con.getInputStream();  
  95.                 byte[] b = new byte[is.available()];  
  96.                 is.read(b);  
  97.                 // System.out.println(new String(b));  
  98.                 System.out  
  99.                         .println("------------------------------------------------------------");  
  100.                 is.close();  
  101.             }  
  102.   
  103.             // 正式登录  
  104.             url = new URL(  
  105.                     "http://192.168.72.130/bbs/post.php?action=newthread&fid="  
  106.                             + new_fid);  
  107.             HttpURLConnection con = (HttpURLConnection) url.openConnection();  
  108.   
  109.             my_cookie = cookie_sid + ";" + cookie_auth;  
  110.             System.out.println(my_cookie);  
  111.             // 加入 Cookie 内容  
  112.             con.setRequestProperty("Cookie", my_cookie);  
  113.   
  114.             if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {  
  115.                 is = con.getInputStream();  
  116.                 byte[] b = new byte[is.available()];  
  117.                 is.read(b);  
  118.                 temp = new String(b);  
  119.                 pos = temp.indexOf("id=\"formhash\" value=");  
  120.                 // System.out.println(temp);  
  121.                 // 获得发帖用的 formhash  
  122.                 post_formhash = temp.substring(pos + 21, pos + 21 + 8);  
  123.                 System.out.println("post_formhash:" + post_formhash);  
  124.                 System.out  
  125.                         .println("------------------------------------------------------------");  
  126. font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px
    分享到:
    评论

相关推荐

Global site tag (gtag.js) - Google Analytics