欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java模拟HTTP Get Post请求 轻松实现校园BBS自动回帖

程序员文章站 2024-03-07 23:26:45
本文实例为大家分享了java模拟http get post请求,校园bbs自动回帖功能,供大家参考,具体内容如下 设计思路 找到帖子链接的集合,最后面数字变化, 就可以...

本文实例为大家分享了java模拟http get post请求,校园bbs自动回帖功能,供大家参考,具体内容如下

设计思路

找到帖子链接的集合,最后面数字变化, 就可以得到不同的帖子

防止帖子发表会又被删了的情况, 进行判断帖子是否存在

遍历这个集合, 对每个链接做回帖的post请求

重难点

note:

  • 回帖需要用户登录信息
  • 一种是利用cookie
  • 另一种是进行模拟登录
  • 本文采用前者

代码

代码比较简单,注意事项是找到自己的cookie,赋给string yourcookeie就可以直接运行

主要就是判断帖子存不存在,这是一个get请求,然后用post发送一个回帖,回帖信息在mapdata.put(“message”, “友情帮顶了”)中 硬编码为”友情帮顶了”,你可以修改

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 inter {

 private static final string baserefer = "http://rs.xidian.edu.cn/forum.php?mod=viewthread&tid=";

 private static final string yourcookeie = "q8qa_2132_saltkey=g1njjj3o; q8qa_2132_lastvisit=1438243699; q8qa_2132_lastcheckfeed=256730%7c1438252008; q8qa_2132_auth=e11aehhxplgtypfdk72yjzegjhl1v70cuxxdtj71vbu2dyuh%2bqhw3pgojhsfxfjbvgnsvyfg1v%2bqld0lt8kg6j%2b40w0; q8qa_2132_st_t=256730%7c1438571068%7c51f8a322985e44f65ff1143329e6779a; q8qa_2132_forum_lastvisit=d_106_1438571068; q8qa_2132_myrepeat_rr=r0; q8qa_2132_ulastactivity=d7degfmawg5aghshmt%2bwcq1l91znqpea57p%2f0vt7vhdc8drougtt; q8qa_2132_home_diymode=1; tjpctrl=1438781938176; q8qa_2132_visitedfid=72d551d215d110d13d142d22d91d217d548; q8qa_2132_st_p=256730%7c1438781224%7c7a73ef608dc3caf733308d63639b3bd0; q8qa_2132_viewid=tid_773850; q8qa_2132_smile=10d1; q8qa_2132_sid=znfqqn; q8qa_2132_lastact=1438781403%09forum.php%09ajax";

 public static void main(string[] args) {
  int startid = 774210; // you need change

  for (int i = 0; i < 100; i++) {
   postmessage(startid);
   startid++;
  }
 }

 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;

 }

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

}

效果图

Java模拟HTTP Get Post请求 轻松实现校园BBS自动回帖

以上就是本文的全部内容,希望对大家的学习有所帮助,实现帖子自动回复。