java.net.http.httpclient不发布对服务器的数据?
我正在尝试写一个简单的HTTP客户端&使用新的(JDK 11)httpclient的服务器。
JDK。:OpenJDK 17.0.0
客户端正在连接到服务器,但似乎并未发布任何数据。
服务器接收到传入连接后,它尝试读取数据。
读取永无止境。
有人知道我在做什么错吗?
服务器来源:
package de.ipp.client.server;
import java.io.*;
import java.net.*;
import java.time.ZonedDateTime;
public class IppServer {
public static final int PORT = 31613;
public static void main(String[] args) throws IOException {
System .out.println(ZonedDateTime.now() + " IppServer Start.....");
try(final ServerSocket serverSocket = new ServerSocket(PORT))
{
System.out.println(ZonedDateTime.now() + " IppServer Running..: " + serverSocket);
while (true) {
final Thread thread = new Thread(getSocketRunnable(serverSocket.accept()));
; thread.setDaemon(true);
; thread.start();
}
}
}
private static Runnable getSocketRunnable(final Socket socket) {
return () -> {
System .out.println(ZonedDateTime.now() + " IppServer Socket...: incoming -> " + socket);
try(final InputStream ist = socket.getInputStream())
{
System.out.println(ZonedDateTime.now() + " IppServer Reading...");
final byte[] bytes = ist.readAllBytes();
System.out.println(ZonedDateTime.now() + " IppServer L'Bytes..: " + bytes.length);
}
catch (final IOException e) {
System.out.println(ZonedDateTime.now() + " IppServer Error!!..: " + e.getMessage());
}
};
}
}
客户来源:
package de.ipp.client.server;
import java.net.URI;
import java.net.http.*;
import java.net.http.HttpClient.Version;
import java.net.http.HttpRequest.*;
import java.net.http.HttpResponse.BodyHandlers;
import java.time.ZonedDateTime;
public class IppClient {
public static void main(final String[] args) throws Exception {
final URI uri = new URI("http://localhost:" + IppServer.PORT + "/printers");
final BodyPublisher publisher = HttpRequest.BodyPublishers.ofByteArray("SomeBytes".getBytes());
final Builder builder = HttpRequest.newBuilder();
; builder.uri (uri);
; builder.version (Version.HTTP_1_1);
; builder.setHeader("Transfer-Encoding", "chunked");
; builder.setHeader("Content-Type", "application/ipp");
; builder.setHeader("Accept-Encoding", "gzip,deflate");
; builder.POST(publisher);
final HttpRequest request = builder.build();
System.out.println(ZonedDateTime.now() + " IppClient Posting..: " + uri);
final HttpResponse<byte[]> response = HttpClient.newHttpClient().send(request, BodyHandlers.ofByteArray());
System.out.println(ZonedDateTime.now() + " IppClient Response.: " + response.statusCode());
}
}
日志:
2022-06-02T11:52:23.378674200+02:00[Europe/Berlin] IppServer Start.....
2022-06-02T11:52:23.411587200+02:00[Europe/Berlin] IppServer Running..: ServerSocket[addr=0.0.0.0/0.0.0.0,localport=31613]
2022-06-02T11:53:31.229082300+02:00[Europe/Berlin] IppServer Socket...: incoming -> Socket[addr=/127.0.0.1,port=55978,localport=31613]
2022-06-02T11:53:31.232074700+02:00[Europe/Berlin] IppServer Reading...
2022-06-02T11:53:30.474583800+02:00[Europe/Berlin] IppClient Posting..: http://localhost:31613/printers
I'm trying to write a simple HTTP Client & Server using the new (JDK 11) HttpClient.
JDK.: OpenJDK 17.0.0
The Client is connecting to the Server but doesn't seem to be POST'ing any data.
After the Server receives the incoming Connection, it tries to read the data.
The read never ends.
Does anyone know what I'm doing wrong?
Server Source:
package de.ipp.client.server;
import java.io.*;
import java.net.*;
import java.time.ZonedDateTime;
public class IppServer {
public static final int PORT = 31613;
public static void main(String[] args) throws IOException {
System .out.println(ZonedDateTime.now() + " IppServer Start.....");
try(final ServerSocket serverSocket = new ServerSocket(PORT))
{
System.out.println(ZonedDateTime.now() + " IppServer Running..: " + serverSocket);
while (true) {
final Thread thread = new Thread(getSocketRunnable(serverSocket.accept()));
; thread.setDaemon(true);
; thread.start();
}
}
}
private static Runnable getSocketRunnable(final Socket socket) {
return () -> {
System .out.println(ZonedDateTime.now() + " IppServer Socket...: incoming -> " + socket);
try(final InputStream ist = socket.getInputStream())
{
System.out.println(ZonedDateTime.now() + " IppServer Reading...");
final byte[] bytes = ist.readAllBytes();
System.out.println(ZonedDateTime.now() + " IppServer L'Bytes..: " + bytes.length);
}
catch (final IOException e) {
System.out.println(ZonedDateTime.now() + " IppServer Error!!..: " + e.getMessage());
}
};
}
}
Client Source:
package de.ipp.client.server;
import java.net.URI;
import java.net.http.*;
import java.net.http.HttpClient.Version;
import java.net.http.HttpRequest.*;
import java.net.http.HttpResponse.BodyHandlers;
import java.time.ZonedDateTime;
public class IppClient {
public static void main(final String[] args) throws Exception {
final URI uri = new URI("http://localhost:" + IppServer.PORT + "/printers");
final BodyPublisher publisher = HttpRequest.BodyPublishers.ofByteArray("SomeBytes".getBytes());
final Builder builder = HttpRequest.newBuilder();
; builder.uri (uri);
; builder.version (Version.HTTP_1_1);
; builder.setHeader("Transfer-Encoding", "chunked");
; builder.setHeader("Content-Type", "application/ipp");
; builder.setHeader("Accept-Encoding", "gzip,deflate");
; builder.POST(publisher);
final HttpRequest request = builder.build();
System.out.println(ZonedDateTime.now() + " IppClient Posting..: " + uri);
final HttpResponse<byte[]> response = HttpClient.newHttpClient().send(request, BodyHandlers.ofByteArray());
System.out.println(ZonedDateTime.now() + " IppClient Response.: " + response.statusCode());
}
}
Logs:
2022-06-02T11:52:23.378674200+02:00[Europe/Berlin] IppServer Start.....
2022-06-02T11:52:23.411587200+02:00[Europe/Berlin] IppServer Running..: ServerSocket[addr=0.0.0.0/0.0.0.0,localport=31613]
2022-06-02T11:53:31.229082300+02:00[Europe/Berlin] IppServer Socket...: incoming -> Socket[addr=/127.0.0.1,port=55978,localport=31613]
2022-06-02T11:53:31.232074700+02:00[Europe/Berlin] IppServer Reading...
2022-06-02T11:53:30.474583800+02:00[Europe/Berlin] IppClient Posting..: http://localhost:31613/printers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
再次感谢 @dave_thompson_085对ReadAllBytes的评论。
这是一个很好的帮助。
这不是写一个HTTP服务器的目的,只需找到Cups4J在线路上发送的内容&amp;基于新的(JDK 11)httpclient编写客户端以替换它。
这是我修改的资源&amp;一些日志记录输出...
客户端来源:
服务器来源:
日志:
Once again, many thanks to @dave_thompson_085 for the comments regarding readAllBytes.
That was a great help.
It was not the intention to write an HTTP-Server, just find exactly what CUPS4J was sending over the line & write a Client based on the new (JDK 11) HttpClient to replace it.
Here are my modified sources & some logging output...
Client Source:
Server Source:
Logs: