被动接收HTTP推送的数据

业务背景:作为服务端,被动接收客户端推送过来的数据。
被动接收http传送的数据,作为服务端,在springboot的项目上实现。在yml或者properties文件中进行ip,path,port设置。
yml文件:

port: 6666
ip: 127.0.0.1
path: /test

业务实现(这里只支持http,已验证),另外为每一个请求建立一个线程进行处理,防止多个请求需要排队等待:

        try {
        	 //实现HTTP SERVER 
            HttpServer server = HttpServer.create(new InetSocketAddress(ip, Integer.parseInt(port)), 0);
            server.createContext(path, new HttpHandler() {
                @Override
                public void handle(final HttpExchange httpExchange) throws IOException {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            if (httpExchange.getRequestMethod().equals(method)) {
                                try {
                                    InputStream is = httpExchange.getRequestBody();
                                    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                                    StringBuilder sb = new StringBuilder();
                                    String line;
                                    while ((line = reader.readLine()) != null) {
                                        sb.append(line).append("\r\n");
                                    }
                                    //获取传送的值
                                    System.out.println(sb.toString());
                                    String response = "success";
//                                System.out.println("success");
                                    httpExchange.sendResponseHeaders(200, response.length());
                                    OutputStream os = httpExchange.getResponseBody();
                                    os.write(response.getBytes());
                                    is.close();
                                    os.close();
                                }catch (IOException e){
                                    e.printStackTrace();
                                }
                            }
                        }
                    }).start();
                }
            });
            server.setExecutor(null);
            server.start();
        } catch (IOException e) {
            e.printStackTrace();
        }

使用postman进行测试,success。

延伸 - 支持https(未验证):

        try {
             //实现HTTPS SERVER   
            HttpsServer hss = HttpsServer.create(new InetSocketAddress(ip, Integer.parseInt(port)),0);  //设置HTTPS端口
            KeyStore ks = KeyStore.getInstance("JKS");   //建立证书库   
            ks.load(new FileInputStream("证书名" ), "密码");  //载入证书   
            KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");  //建立一个密钥管理工厂   
            kmf.init(ks, "密码");  //初始工厂   
            SSLContext sslContext = SSLContext.getInstance("SSLv3");  //建立证书实体   
            sslContext.init(kmf.getKeyManagers(), null, null);   //初始化证书   
            HttpsConfigurator conf = new HttpsConfigurator(sslContext);  //在https配置   
            hss.setHttpsConfigurator(conf);   //在https server载入配置 
            
            hss .setExecutor(null);
            hss .createContext(path, new HttpHandler() {
                @Override
                public void handle(final HttpExchange httpExchange) throws IOException {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            if (httpExchange.getRequestMethod().equals(method)) {
                                try {
                                    InputStream is = httpExchange.getRequestBody();
                                    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                                    StringBuilder sb = new StringBuilder();
                                    String line;
                                    while ((line = reader.readLine()) != null) {
                                        sb.append(line).append("\r\n");
                                    }
                                    //获取传送的值
                                    System.out.println(sb.toString());
                                    String response = "success";
//                                System.out.println("success");
                                    httpExchange.sendResponseHeaders(200, response.length());
                                    OutputStream os = httpExchange.getResponseBody();
                                    os.write(response.getBytes());
                                    is.close();
                                    os.close();
                                }catch (IOException e){
                                    e.printStackTrace();
                                }
                            }
                        }
                    }).start();
                }
            });
            hss .setExecutor(null);
            hss .start();
        } catch (IOException e) {
            e.printStackTrace();
        }

参考:
https://blog.csdn.net/qq_405930170/article/details/70770095

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值