118 http 协议中如何实现流式的交互数据

前言

我们经常 会碰到的 基于 http 流式交互的数据方式

一般在 现在常见的大模型中, 一般是基于 websocket 来进行流式交互

这里 来介绍一下 基于 http 的流式数据交互

基于 http 协议的流式数据交互 - 服务器

主要是基于 println 和 flush

复制代码
@RestController
@RequestMapping("/HelloWorld")
public class HelloWorldController {

    @PostMapping("/flowResponse")
    public void flowResponse(HttpServletResponse response) throws Exception {
        response.setContentType("text/event-stream");
        response.setCharacterEncoding("utf8");
        PrintWriter pw = response.getWriter();

        for (int i = 1; i < 10; i++) {
            pw.println(String.format("this is section %s", i));
            pw.flush();
            Thread.sleep(1000);
        }
    }

}

基于 http 协议的流式数据交互 - 客户端

发送请求这边的处理如下

复制代码
/**
 * Test26PostFlowRequest
 *
 * @author Jerry.X.He <970655147@qq.com>
 * @version 1.0
 * @date 2024-07-30 21:29
 */
public class Test26PostFlowRequest {

    // Test26PostFlowRequest
    public static void main(String[] args) {

        String url = "http://localhost:8080/HelloWorld/flowResponse";
        Map<String, String> headers = URLConnectionUtils.commonHeaders();
        String postBody = "{}";
        URLConnectionUtils.postBodyWithFlow(url, headers, postBody, "utf8", line -> {
            System.out.println(line);
        });

    }

}

postBodyWithFlow 的处理如下

发送 http 请求, 然后 一行一行的处理接收到的数据

复制代码
    /**
     * postBodyWithFlow
     *
     * @return java.lang.String
     * @author Jerry.X.He
     * @date 2024/7/30 20:26
     */
    public static HttpResponse postBodyWithFlow(String urlStr, Map<String, String> headers, String postBody,
                                            String respCharset, Consumer<String> func) {
        HttpURLConnection conn = null;
        try {
            disableSSLVerification();
            URL url = new URL(urlStr);
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");//POST GET PUT DELETE
            for (Map.Entry<String, String> entry : headers.entrySet()) {
                conn.setRequestProperty(entry.getKey(), entry.getValue());
            }
            conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.connect();
            OutputStream os = conn.getOutputStream();
            os.write(postBody.getBytes());
            os.flush();
            os.close();

            HttpResponse result = new HttpResponse();
            result.setUrl(conn.getURL());
            result.setMethod(conn.getRequestMethod());
            result.setHeaders(conn.getHeaderFields());
            result.setResponseCode(conn.getResponseCode());
            result.setResponseMessage(conn.getResponseMessage());

            StringBuilder respFromServer = new StringBuilder();
            // if success, read response from server
            if (conn.getResponseCode() >= 200 && conn.getResponseCode() < 300) {
                if (respCharset == null) {
                    respCharset = getCharsetFromServer(result);
                }

                BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), respCharset));
                String line = null;
                while ((line = reader.readLine()) != null) {
                    func.accept(line);
                    respFromServer.append(line).append("\r\n");
                }
            }

            result.setRespCharset(respCharset);
            result.setRespFromServer(respFromServer.toString());
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
        }
    }

演示效果

一秒中输出一个 "this is section $id"

相关推荐
喵个咪2 天前
Go-Wind HTTP 服务器从入门到精通
后端·http·go
CSharp精选营3 天前
WebSocket 快速入门教程(附示例源码)
websocket·教程·csharp·实时通信·asp.net-core
Goodbye8 天前
大模型无状态架构:从 HTTP 协议到 Harness AI 工程的深度解析
http
霜落长河14 天前
抛弃TCP改用UDP,HTTP3怎么了?
http
之歆15 天前
现代 HTTP 客户端深度解析:Fetch 与 Axios
chrome·网络协议·http
程序员mine15 天前
HTTPS-TLS加密与证书完全指南(下)
网络协议·http·https
七夜zippoe15 天前
DolphinDB WebSocket接入:实时数据流
网络·websocket·网络协议·dolphindb·实时数据流
SomeOtherTime16 天前
http协议处理播放video/mp4视频
http
于先生吖16 天前
从零搭建Java萌宠社交系统:WebSocket实时聊天+动态发布模块实现
java·开发语言·websocket
llz_11216 天前
web-第五次课后作业
前端·后端·http