gpt4 book ai didi

java - 在java中解析http响应字节

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:43:21 24 4
gpt4 key购买 nike

我正在尝试用 java 解析 byte[],它代表 HTTP 响应。有这个问题Is there any simple http response parser for Java? ,这正是我的问题,但接受的答案对我没有帮助。如果我看http://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/io/HttpMessageParser.html ,我不明白这对我有什么帮助。

最佳答案

我希望这能让你开始

String s = "HTTP/1.1 200 OK\r\n" +
"Content-Length: 100\r\n" +
"Content-Type: text/plain\r\n" +
"Server: some-server\r\n" +
"\r\n";
SessionInputBufferImpl sessionInputBuffer = new SessionInputBufferImpl(new HttpTransportMetricsImpl(), 2048);
sessionInputBuffer.bind(new ByteArrayInputStream(s.getBytes(Consts.ASCII)));
DefaultHttpResponseParser responseParser = new DefaultHttpResponseParser(sessionInputBuffer);
HttpResponse response = responseParser.parse();
System.out.println(response);

此代码产生以下输出:

HTTP/1.1 200 OK [Content-Length: 100, Content-Type: text/plain, Server: some-server]

关于java - 在java中解析http响应字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26549951/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com