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
builder.setHeader("Transfer-Encoding", "chunked");? Because that sounds like an option that should be decided by the http client itself, not by you. Same goes probably forbuilder.setHeader("Accept-Encoding", "gzip,deflate");.readAllByteson the socket, which reads until the peer (client) disconnects, but nonancient HTTP (1.0' up) doesn't disconnect and doesn't delimit requests or responses using disconnection. See RFC7230 et seq, or 2616, or wikipedia. Either use something that actually is an HTTP server like com.sun.net.httpserver, Glassfish, Tomcat, Jetty, Netty; or correctly implement HTTP, which is a good deal of work, which is exactly why products like the above exist.