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"

相关推荐
极安代理1 天前
HTTP代理是什么?作用与场景全面解析
网络·网络协议·http
未来之窗软件服务1 天前
幽冥大陆(一百07)—门禁局域网http获取名单—东方仙盟练气期
网络·http·仙盟创梦ide·东方仙盟·东方仙盟智能硬件·智能闸机
ps酷教程1 天前
netty模拟文件列表http服务器
http·netty
ashcn20012 天前
websocket测试通信
前端·javascript·websocket
人道领域2 天前
JavaWeb从入门到进阶(HTTP协议的请求与响应)
网络·网络协议·http
wenjianhai2 天前
WebSocket调试工具---Apifox
网络·websocket·网络协议
REDcker2 天前
WebSocket 协议详解 (RFC 6455)
网络·websocket·网络协议
极安代理2 天前
HTTP代理IP如何提升爬虫采集效率?
爬虫·tcp/ip·http
草根站起来2 天前
https加密证书
网络协议·http·https
蜂蜜黄油呀土豆2 天前
深入理解计算机网络中的应用层知识
计算机网络·http·tcp·网络通信·dns