在 Java 中通过 HttpClient 处理原始 HTTP 标头

发布于 2024-12-10 03:13:37 字数 914 浏览 0 评论 0原文

我的客户端接收原始 HTTP 标头(包括 GET、POST、Multipart POST 等),我想将它们发送到服务器并获取输出。 但我不想手动解析整个请求,然后将所有解析的内容设置为 HttpClient...

是否有一种优雅的方法来执行此操作(甚至类似于下面的代码)?

AGoodHttpClient response = new AGoodHttpClient(host, port, myHeaders);
InputStream in = response.getInputStream();
// ...

已编辑

假设我有这段代码。我如何识别 EOS(-1 不适用于 HTTP/1.1)。传输完成后是否有保证如何切断连接?我想要一些关心切断连接的东西(比如 HttpClient),但可以直接访问发送标头(比如 outToServer.write(myHeaders))。

 Socket connectionToServer = new Socket(host, port);
 OutputStream outToServer = connectionToServer.getOutputStream();

 outToServer.write(myHeaders.getBytes());

 InputStream inputFromServer = connectionToServer.getInputStream();
 byte[] buff = new byte[1024];

 int count;
 while ((count = inputFromServer.read(buff)) != -1) {
       System.out.write(buff, 0, count);
 }

感谢您的帮助!

My client receive raw HTTP headers (including GET, POST, Multipart POST, etc.) and I want to send them to a server and get output.
But I don't want to parse whole request manually, then set all that parsed stuff to HttpClient...

Does an elegant way to do this (even something like code below)?

AGoodHttpClient response = new AGoodHttpClient(host, port, myHeaders);
InputStream in = response.getInputStream();
// ...

Edited

Let's say I have this code. How do I recognize EOS (-1 isn't working for HTTP/1.1). Is there a guaranteed way how to cut the connection, when transfer is done? I want something what will care about cutting a connection (something like HttpClient), but with direct access to sending headers (like outToServer.write(myHeaders)).

 Socket connectionToServer = new Socket(host, port);
 OutputStream outToServer = connectionToServer.getOutputStream();

 outToServer.write(myHeaders.getBytes());

 InputStream inputFromServer = connectionToServer.getInputStream();
 byte[] buff = new byte[1024];

 int count;
 while ((count = inputFromServer.read(buff)) != -1) {
       System.out.write(buff, 0, count);
 }

Thanks for help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文