Java模拟HTTP Get Post请求 轻松实现校园BBS自动回帖
作者:学习编程知识 时间:2021-10-08 17:21:28
本文实例为大家分享了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,HTTPGetPost请求,校园BBS,回帖
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
JAVA8 Stream流中的reduce()方法详解
2023-09-01 21:10:33
C# Winform实现自定义漂亮的通知效果
2021-08-10 08:15:29
SSH框架网上商城项目第10战之搭建商品类基本模块
2023-11-12 14:00:29
![](https://img.aspxhome.com/file/2023/6/59006_0s.png)
C#实现的算24点游戏算法实例分析
2021-12-01 04:13:21
使用Android studio创建的AIDL编译时找不到自定义类的解决办法
2023-06-23 10:59:41
Java 集合中的类关于线程安全
2023-03-13 12:53:22
C++实现的链表类实例
2023-07-04 08:36:36
Java 蒙特卡洛算法求圆周率近似值实例详解
2023-10-19 23:32:10
c# 委托的常见用法
2021-08-11 13:44:05
Spring Boot教程之必须了解的核心概念
2022-07-15 14:17:24
![](https://img.aspxhome.com/file/2023/2/70582_0s.png)
Java设计模式之观察者模式(Observer模式)介绍
2022-10-16 04:40:42
![](https://img.aspxhome.com/file/2023/2/82002_0s.png)
MyBatis中映射文件的使用案例代码
2021-09-02 23:55:41
![](https://img.aspxhome.com/file/2023/0/60910_0s.png)
C#控制台实现飞行棋游戏
2022-03-12 04:18:40
![](https://img.aspxhome.com/file/2023/2/94642_0s.jpg)
Java受检异常的一些思考
2021-06-08 08:22:16
Java编程在ICPC快速IO实现源码
2021-10-18 01:41:54
SpringBoot 在项目启动之后执行自定义方法的两种方式小结
2021-05-25 15:46:36
![](https://img.aspxhome.com/file/2023/8/71168_0s.jpg)
DevExpress根据条件设置GridControl RepositoryItem是否可编辑
2023-03-21 14:53:30
![](https://img.aspxhome.com/file/2023/0/92960_0s.png)
Java中Synchronized的用法解析
2023-07-28 22:40:51
![](https://img.aspxhome.com/file/2023/7/62227_0s.png)
Java 判断实体对象及所有属性是否为空的操作
2022-12-06 14:32:07
Unity3D快速入门教程
2022-04-03 14:11:14
![](https://img.aspxhome.com/file/2023/1/94541_0s.png)