Reputation: 2050
We have a java server application, who is listening on port 5545 for http communciation.
On the other side is a C# Client application, who sends requests to the server.
Before some problems with proxies occured, our communication was based on TCP/IP.
Now we wanna switch to a communication via http, but we experienced some strange behaviour on the c# side.
The problem is based on the PostAsync Method
Our Java Server is using the Netty 4 Framework with Spring. When the c# client post the data, i can see everything in the netty logs. Now at the part, where the java server sends the response back, the c# method is going into something like a deadlock.
My java method for sending the response is
private boolean writeResponse(boolean isSuccessful, String message, ChannelHandlerContext ctx) {
boolean keepAlive = HttpHeaders.isKeepAlive(_httpRequest);
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
isSuccessful ? HttpResponseStatus.OK : HttpResponseStatus.BAD_REQUEST,
Unpooled.copiedBuffer(message, CharsetUtil.UTF_8));
response.headers().set(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP);
response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/octet-stream; charset=UTF-8");
if (keepAlive) {
response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, message.length());
response.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
}
ctx.write(response);
return keepAlive;
}
The c# client code is
HttpClientHandler httpClientHandler = new HttpClientHandler();
client = new System.Net.Http.HttpClient(httpClientHandler) { BaseAddress = uri };
byte[] bytes = ...
ByteArrayContent byteContent = new ByteArrayContent(bytes);
var response = await client.PostAsync(uri, byteContent, tokenSource.Token);
When i add ctx.channel().writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
on the java side after the last write. The PostAsync Method gets a response, otherwise it is just stuck, although the java server send the response already.
So my question is, is this a normal behaviour or can someone explain me, what we are doing wrong?
Upvotes: 1
Views: 148
Reputation: 465
You should set CONTENT_LENGTH everytime, not only when keepAlive is true.
[Edit] Otherwise the client does not know if and when response-content is fully received and the post-method will not return.
Upvotes: 1