机器人下载
发送消息
http://127.0.0.1:5700/send_msg?message_type=private&user_id=911412667&message=你好呀
检查端口是否打开
netstat -ano | findstr :5701
发送的请求
软件的dopost的解析
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // 设置编码格式
- System.out.println("你好呀我获取了数据");
- request.setCharacterEncoding("UTF-8");
- // response.setContentType("text/json; charset=utf-8"); //设置编码格式和数据类型
- response.setHeader("Content-Type", "application/json;charset=UTF-8");
-
-
- // 获取请求体中的数据
- BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream(),StandardCharsets.UTF_8)); //第一种方式
- // String requestBody = IOUtils.toString(request.getInputStream(), String.valueOf(StandardCharsets.UTF_8));//第二种方式
- // System.out.println("错误乱码检查:"+requestBody);
-
-
- String jsonStr = "";
- String line = null;
- while ((line = reader.readLine()) != null) {
- jsonStr += line;
- }
- System.out.println(jsonStr);
-
- // // 解析JSON格式的数据
- // Gson gson = new Gson();
- // Message message = gson.fromJson(jsonStr, Message.class);
- //
- // // 输出解析结果
- // System.out.println(message.getMessage());
-
-
- }
需要的jar包
commons的
-
commons-fileupload -
commons-fileupload -
1.3.3
json
-
com.alibaba -
fastjson -
1.2.76
通过WebSocket通信
- package WebSocket.Server;
-
- import java.net.URI;
- import javax.websocket.ClientEndpoint;
- import javax.websocket.OnError;
- import javax.websocket.OnMessage;
- import org.glassfish.tyrus.client.ClientManager;
-
- @ClientEndpoint
- public class WebsocketClient {
- public static void main(String[] args) throws Exception {
- String serverEndpoint = "ws://127.0.0.1:8081"; // 这里设置 Websocket 服务器的地址
-
- ClientManager client = ClientManager.createClient();
- WebsocketClient socket = new WebsocketClient();
- client.connectToServer(socket, new URI(serverEndpoint));
-
- // 等待接收服务器发来的消息
- while (true) {
- Thread.sleep(1000);
- }
- }
-
- @OnMessage
- public void onMessage(String message) {
- System.out.println("Received message from server: " + message);
- }
-
- @OnError
- public void onError(Throwable t) {
- t.printStackTrace(System.err);
- }
- }
所有的jar包
- <dependencies>
- <dependency>
- <groupId>org.thymeleafgroupId>
- <artifactId>thymeleafartifactId>
- <version>3.0.15.RELEASEversion>
- dependency>
- <dependency>
- <groupId>com.alibabagroupId>
- <artifactId>fastjsonartifactId>
- <version>1.2.76version>
- dependency>
-
- <dependency>
- <groupId>org.apache.maven.surefiregroupId>
- <artifactId>surefire-booterartifactId>
- <version>2.22.2version>
- dependency>
- <dependency>
- <groupId>commons-fileuploadgroupId>
- <artifactId>commons-fileuploadartifactId>
- <version>1.3.3version>
- dependency>
-
- <dependency>
- <groupId>javax.websocketgroupId>
- <artifactId>javax.websocket-client-apiartifactId>
- <version>1.1version>
- dependency>
-
- <dependency>
- <groupId>org.glassfish.tyrus.bundlesgroupId>
- <artifactId>tyrus-standalone-clientartifactId>
- <version>1.17version>
- dependency>
- <dependency>
- <groupId>org.slf4jgroupId>
- <artifactId>jul-to-slf4jartifactId>
- <version>1.7.36version>
- dependency>
- <dependency>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- <version>1.18.20version>
- <scope>providedscope>
- dependency>
-
- dependencies>