I wrote an HTTP server in Java and a C ++ client with Poco. This is part of the C ++ client code:
URI uri("http://127.0.0.1:4444"); HTTPClientSession session(uri.getHost(), uri.getPort()); HTTPRequest req(HTTPRequest::HTTP_POST, "/pages/page", HTTPMessage::HTTP_1_1); session.sendRequest(req); HTTPResponse res; std::istream &is = session.receiveResponse(res);
In the last line, I get the following error:
terminate called after throwing an instance of 'Poco::Net::NoMessageException' what(): No message received
But I do not understand why. The connection was successful and the requested page exists. I tried the same code with famous websites (like Wikipedia) and it works without any exceptions.
I also tried to make the exact same request with cURL (to my server) on the command line, and it displays the server response, so the server seems fine.
This is the original server response in string form:
"HTTP/1.1 200 OK\r\n" + "Server: [server name]\r\n" + "Content-Type: text/xml; charset=utf-8\r\n" + "Content-Length:" + bodyBytes.length + "\r\n" + "Resource: " + job.resId + "\r\n\r\n" + "<?xml version=\"1.0\" encoding=\"UTF-8\"?><JobRequest><InputRepresentation id=\"0\"/> <effectsList><cvtColor><code> CV_RGB2GRAY </code></cvtColor><resize><scaleFactorX> 0.5 </scaleFactorX><scaleFactorY> 0.5 </scaleFactorY><interpolation> INTER_LINEAR </interpolation></resize><GaussianBlur><kSize> 3 </kSize><sigmaX> 2 </sigmaX><sigmaY> 2 </sigmaY><borderType> BORDER_REPLICATE </borderType></GaussianBlur></effectsList></JobRequest>"
I wrote a simple HTTP server that responds with a fixed response to each request to check what is wrong. this is the code:
public class Test { public static void main(String[] args) { try { ServerSocket serverSocket = new ServerSocket(4449); Socket clientSocket = serverSocket.accept(); String body = "ab"; byte[] bodyBytes = body.getBytes("UTF-8"); String headers = "HTTP/1.1 200 OK\r\n" + "Server: Foo\r\n" + "Content-Type: text/plain\r\n" + "Content-Length: " + bodyBytes.length + "\r\n\r\n"; byte[] headerBytes = headers.getBytes("UTF-8"); byte[] responseBytes = new byte[headerBytes.length + bodyBytes.length]; int i = 0; for (int j = 0; j < headerBytes.length; ++j) { responseBytes[i] = headerBytes[j]; ++i; } for (int j = 0; j < bodyBytes.length; ++j) { responseBytes[i] = bodyBytes[j]; ++i; } clientSocket.getOutputStream().write(responseBytes); } catch (IOException e) {} } }
I get the same exception even with this server. So what is wrong here?
source share