[英]Poco::HttpClientSession.receiveResponse() throws NoMessageException without any apparent reason
I wrote an HTTP server in Java and a client in C++ with Poco . 我用Poco用Java编写了HTTP服务器,并用C ++编写了客户端。 This is a part of the C++ client code:
这是C ++客户端代码的一部分:
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 don't understand why. 但是我不明白为什么。 The connection was established successfully and the page requested exists.
连接成功建立,请求的页面存在。 I tried the same code with known websites (like Wikipedia) and it works without any exception.
我在已知的网站(例如Wikipedia)上尝试了相同的代码,并且没有任何异常。
I also tried to make the exact same request with cURL (to my server) in command-line and it shows the response of the server, so the server seems fine. 我还尝试在命令行中使用cURL向我的服务器发出完全相同的请求,它显示了服务器的响应,因此服务器看起来不错。
This is the original response of the server in a 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 have written a simple HTTP server which respond with a fixed response for every request, to test what's wrong. 我编写了一个简单的HTTP服务器,该服务器针对每个请求以固定的响应进行响应,以测试出了什么问题。 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];
/* Fill responseBytes with the header and body bytes */
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's wrong here?
那这怎么了?
Based on experimentation, I've found that it's necessary to include a Content-Length
header if you are making an empty POST request. 根据实验,我发现如果要发出空的POST请求,则必须包含
Content-Length
标头。 Ie, 即
req.add("Content-Length", "0");
I had to use the following sequence in my handler code for a minimal response to be received without a No message received
error: 我必须在处理程序代码中使用以下序列,以使收到的响应最少,而不会收到“
No message received
错误:
resp.setStatus( Poco::Net::HTTPResponse::HTTP_OK );
resp.setContentType( "text/json" );
ostream &out = resp.send();
out << "[]";
out.flush();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.