当前位置: 移动技术网 > IT编程>开发语言>Java > Java模拟HTTP Get Post请求实现论坛自动回帖功能

Java模拟HTTP Get Post请求实现论坛自动回帖功能

2019年07月22日  | 移动技术网IT编程  | 我要评论

最近想自动发帖回帖,拿某论坛试验了一下,发现可行,不过后续没有再使用,以免影响论坛正常运行。

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

效果图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网