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



设计思路

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

  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;

}

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

模拟发帖

代码比较简单,注意事项是找到自己的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();
    }
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77

还有一个工具方法,将输入流转化为字节

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();
    }
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

效果图

www.alijava.com

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值