本文用于记录一个简单的自动论坛回帖功能,只能用于一般DZ论坛,在河畔上做过测试,功能比较简单,基于HTTP1.0
而且是绑定了我自己的账号与电脑。后续可以试着实现其他功能。
</pre><p>直接上代码吧!</p><p><pre name="code" class="java">package com.son.web;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* 实现了在河畔回帖功能
*
*/
public class Test {
//帖子的URL 具体id可以作为入参
private static final String baseRefer = "http://bbs.uestc.edu.cn/forum.php?mod=viewthread&tid=";
//通过开发者工具获得的本地cookie
private static final String yourCookeie = "v3hW_2132_saltkey=Uj7l9d5g; v3hW_2132_lastvisit=1450871500; v3hW_2132_sid=TIoPdX; v3hW_2132_lastact=1451977468%09forum.php%09viewthread; CNZZDATA5360190=cnzz_eid%3D547165112-1451959228-%26ntime%3D1451975428; v3hW_2132_ulastactivity=63f2skBWtcnzJIIAUHE8ou2tooEJTjNreEpv4RJF37z8QoSlX9Jr; v3hW_2132_auth=3dbay%2FG7ssiwhHKEWqj4fu8qPrD2Uej9p3OsH1hHgHwVky41alb4M6s4rXT4C4dXcw5Ekw%2B9L7Hfqzo51HfQV03NCjM; v3hW_2132_lastcheckfeed=137810%7C1451962090; v3hW_2132_nofavfid=1; tjpctrl=_p=137810%7C1451977468%7C84a4d6e232dce38abc5523f318a2693a; v3hW_2132_viewid=tid_1583177; v3hW_2132_clearUserdata=forum; v3hW_2132_creditnotice=0D0D0D0D0D0D0D0D0D137810; v3hW_2132_creditbase=0D1D824D0D160000D0D0D0D0; v3hW_2132_creditrule=%E5%8F%91%E8%A1%A8%E5%9B%9E%E5%A4%8D";
public static void main(String[] args) {
int startId = 1583188; // 帖子id
for (int i = 0; i < 10; i++) {
postMessage(startId);
startId++;
}
}
/*
检查所访问帖子是否存在
*/
public static boolean isExist(int id) {
String tmpPath = baseRefer + id;
URL url;
try {
//通过开发者工具找到的一些参数
//键 值User-Agent Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
//键 值Referer http://bbs.uestc.edu.cn/forum.php?mod=viewthread&tid=1583177
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 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");
con.addRequestProperty("Referer", "http://bbs.uestc.edu.cn/forum.php?mod=viewthread&tid=1583177");
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 (Exception e) {
e.printStackTrace();
}
return true;
}
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", "363aef35");
//通过开发者工具可以查找到回帖所需要的一些请求参数
//message=%E7%9C%8B%E7%9C%8B&posttime=1451977412&formhash=363aef35&usesig=1&subject=++
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());
System.out.println(url);
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 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");
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");
//info可以做个输出流输出到浏览器中 这里就算了
System.out.println("在id=" + id + "成功发帖!");
try {
Thread.sleep(20 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
class StreamTool {
public static byte[] read(InputStream inputStr) throws Exception {
ByteArrayOutputStream outStr = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inputStr.read(buffer)) != -1) {
outStr.write(buffer, 0, len);
}
inputStr.close();
return outStr.toByteArray();
}
}