遇到一个 Powered by discuz 7.2 ! 的论坛,有积分的限制,很是不爽,发帖可以增加积分,所以花了点时间琢磨弄个自动发帖机,以下是基本的代码和原理说明,要实现自动发帖,循环执行以下代码即可。(注意发帖的时间间隔限制)
package com.ldq;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;
public class MyNewTopic {
private static URL url;
private static HttpURLConnection con;
private static String temp;
private static InputStream is;
private static byte[] b;
private static int pos;
private static String cookie_sid;
private static String cookie_auth;
private static String my_cookie;
private static String login_formhash;
private static String post_formhash;
private static String user = "test";// 用户名
private static String pass = "test";// 密码
private static String new_fid = "11";// 版块 ID
private static String subject = "新主题";// 标题
private static String msg = "这里是新主题的内容";// 帖子内容
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
// 获取 cookie_sid 和 login_formhash --------------------
url = new URL("http://192.168.72.130/bbs/logging.php?action=login");
con = (HttpURLConnection) url.openConnection();
if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {
// 获取服务器发给客户端的 Cookie
temp = con.getHeaderField("Set-Cookie");
System.out.println("Set-Cookie:" + temp);
// 取 Cookie 前面的部分就可以了,后面是过期时间、路径等,不用管它
cookie_sid = temp.substring(0, 14);
System.out.println(cookie_sid);
is = con.getInputStream();
b = new byte[is.available()];
is.read(b);
// 服务器会返回一个页面,此页面中包含 formhash
temp = new String(b);
// 找出这个 formhash 的位置
pos = temp.indexOf("name=\"formhash\" value=");
// System.out.println(temp);
// 找出这个 formhash 的内容,这是登录用的 formhash
login_formhash = temp.substring(pos + 23, pos + 23 + 8);
System.out.println("login_formhash:" + login_formhash);
System.out
.println("------------------------------------------------------------");
is.close();
}
// 获取cookie_auth -----------------------------------------------
url = new URL("http://192.168.72.130/bbs/logging.php");
con = (HttpURLConnection) url.openConnection();
// 设定以 POST 发送
con.setRequestMethod("POST");
// 加入 Cookie 内容
con.setRequestProperty("Cookie", cookie_sid);
// 添加 POST 的内容
con.setDoOutput(true);
OutputStreamWriter osw = new OutputStreamWriter(con
.getOutputStream());
osw
.write("action=login&loginfield=username&questionid=0&answer=&loginsubmit=yes&formhash="
+ login_formhash
+ "&username="
+ user
+ "&password=" + pass);
osw.flush();
osw.close();
if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {
Map<String, List<String>> map = con.getHeaderFields();
List<String> list = map.get("Set-Cookie");
for (int i = 0; i < list.size(); i++) {
temp = list.get(i);
if (temp.contains("CsN_auth")) {
System.out.println(temp);
// 取 Cookie 前面的部分就可以了,后面是过期时间、路径等,不用管它
cookie_auth = temp.split(";")[0];
System.out.println("cookie_auth:" + cookie_auth);
}
}
is = con.getInputStream();
byte[] b = new byte[is.available()];
is.read(b);
// System.out.println(new String(b));
System.out
.println("------------------------------------------------------------");
is.close();
}
// 正式登录
url = new URL(
"http://192.168.72.130/bbs/post.php?action=newthread&fid="
+ new_fid);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
my_cookie = cookie_sid + ";" + cookie_auth;
System.out.println(my_cookie);
// 加入 Cookie 内容
con.setRequestProperty("Cookie", my_cookie);
if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {
is = con.getInputStream();
byte[] b = new byte[is.available()];
is.read(b);
temp = new String(b);
pos = temp.indexOf("id=\"formhash\" value=");
// System.out.println(temp);
// 获得发帖用的 formhash
post_formhash = temp.substring(pos + 21, pos + 21 + 8);
System.out.println("post_formhash:" + post_formhash);
System.out
.println("------------------------------------------------------------");
is.close();
}
// 发新帖子
url = new URL("http://192.168.72.130/bbs/post.php");
con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Cookie", my_cookie);
con.setDoOutput(true);
osw = new OutputStreamWriter(con.getOutputStream());
temp = "action=newthread&topicsubmit=yes&subject=" + subject
+ "&formhash=" + post_formhash + "&fid=" + new_fid
+ "&message=" + msg;
osw.write(temp);
osw.flush();
osw.close();
if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {
is = con.getInputStream();
byte[] b = new byte[is.available()];
is.read(b);
// System.out.println(new String(b));
System.out
.println("------------------------------------------------------------");
is.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
正好 2010-01-06 22:01,太晚了,再不走赶不上末班地铁了,原理下次再发帖详细说明吧。
分享到:
相关推荐
Discuz_7.2_FULL_SC_UTF8.zip Discuz_7.2_FULL_SC_UTF8.zip Discuz_7.2_FULL_SC_UTF8.zip Discuz_7.2_FULL_SC_UTF8.zip
DISCUZ7.2ENGLISHBYCOOBY.rar DISCUZ7.2英文版,适合国外的
红色风格模板最新discuz模板discuz7.2用的
Discuz7.2 Water3风格 1. 1、将附件下载后解压缩到当前文件夹,得到一个“water7.2”文件夹; 2. 2、将这个文件夹拷贝到你的论坛的“templates”目录下; 3. 3、登录到论坛后台,到“界面”→“风格管理”,在...
Discuz_7.2 Ucenter整合
复现discuz7.2注入漏洞
dz 7.2的数据字典。有了它还怕不懂dz吗?
最经典Discuz!7.2源码分析 最早由郭鑫在discuz.net上发贴 后无经无数人转载. 本人整理为txt.
论坛风格模板Discuz模板是一款用于社区 蓝色 论坛 交流的模板,适用于discuz! x2.5版本。
discuz 7.2-UTF8(人人连接组件) discuz 7.2-UTF8(人人连接组件) discuz 7.2-UTF8(人人连接组件)
Discuz7.2在线阅读插件。主要针对swf格式所写。下载后直接覆盖到论坛根目录即可。
discuz7.2首页多格插件discuz7.2首页多格插件discuz7.2首页多格插件discuz7.2首页多格插件discuz7.2首页多格插件discuz7.2首页多格插件discuz7.2首页多格插件
全新推出的Discuz! 7.2版本从用户注册登陆应用、社区论坛前后管理两方面着眼,增加和改进了30多处与社区门户运营密切相关的功能与细节,实现了邮箱登陆、分类信息检索、版块绑定域名等功能。
discuz!7.2数据库结构表 discuz 源码 utf8格式 环境搭建使用。
Discuz!7.2用户手册..................
dz7.2插件dz7.2插件dz7.2插件dz7.2插件
Discuz! 7.2活动召集美化插件 插件简:活动召集插件美化版
为了把握当前主流社区产品的应用趋势,满足 地方及垂直社区门户化、规模化发展的应用需求,康盛创想官方开发团队通过需求调研和走访交流,与全国各地数千名站长面对面的对话与沟通,在Discuz! 7.1版本的基础之上完成...
为了把握当前主流社区产品的应用趋势,满足 地方及垂直社区门户化、规模化发展的应用需求,康盛创想官方开发团队通过需求调研和走访交流,与全国各地数千名站长面对面的对话与沟通,在Discuz! 7.1版本的基础之上完成...
discuz7.2 模板,;里面是一个discuz制作的模板,分享下