Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Poco::HttpClientSession.ReceiverResponse()在没有任何明显原因的情况下抛出NoMessageException 我用java编写了HTTP服务器,C++用Poco编写了一个客户端。这是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);_C++_Http_Poco Libraries - Fatal编程技术网

Poco::HttpClientSession.ReceiverResponse()在没有任何明显原因的情况下抛出NoMessageException 我用java编写了HTTP服务器,C++用Poco编写了一个客户端。这是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);

Poco::HttpClientSession.ReceiverResponse()在没有任何明显原因的情况下抛出NoMessageException 我用java编写了HTTP服务器,C++用Poco编写了一个客户端。这是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);,c++,http,poco-libraries,C++,Http,Poco Libraries,在最后一行中,我得到以下错误: terminate called after throwing an instance of 'Poco::Net::NoMessageException' what(): No message received 但我不明白为什么。连接已成功建立,请求的页面已存在。我在已知网站(如维基百科)上尝试了相同的代码,它毫无例外地工作 我还尝试在命令行中使用cURL(对我的服务器)发出完全相同的请求,它显示了服务器的响应,因此服务器看起来很好 这是服务器以字符串形

在最后一行中,我得到以下错误:

terminate called after throwing an instance of 'Poco::Net::NoMessageException'
  what():  No message received
但我不明白为什么。连接已成功建立,请求的页面已存在。我在已知网站(如维基百科)上尝试了相同的代码,它毫无例外地工作

我还尝试在命令行中使用cURL(对我的服务器)发出完全相同的请求,它显示了服务器的响应,因此服务器看起来很好

这是服务器以字符串形式的原始响应:

"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>"
我编写了一个简单的HTTP服务器,它对每个请求都有一个固定的响应,以测试错误。代码如下:

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) {}
    }
}
公共类测试{
公共静态void main(字符串[]args){
试一试{
ServerSocket ServerSocket=新的ServerSocket(4449);
Socket clientSocket=serverSocket.accept();
字符串体=“ab”;
byte[]bodyBytes=body.getBytes(“UTF-8”);
String headers=“HTTP/1.1 200正常\r\n”+
“服务器:Foo\r\n”+
“内容类型:文本/普通\r\n”+
内容长度:“+bodyBytes.Length+”\r\n\r\n”;
byte[]headerBytes=headers.getBytes(“UTF-8”);
byte[]responseBytes=新字节[headerBytes.length+bodyBytes.length];
/*用标头和正文字节填充responseBytes*/
int i=0;
对于(int j=0;j

即使使用此服务器,我也会遇到相同的异常。那么这里出了什么问题呢?

根据实验,我发现如果您发出一个空的POST请求,那么有必要包含一个
内容长度
标题。即

req.add("Content-Length", "0");

我必须在我的处理程序代码中使用以下序列,以便在不出现
未收到消息的情况下接收到最小响应
错误:

resp.setStatus( Poco::Net::HTTPResponse::HTTP_OK );
resp.setContentType( "text/json" );
ostream &out = resp.send();
out << "[]";
out.flush();
响应设置状态(Poco::Net::HTTPResponse::HTTP_OK); 分别为setContentType(“text/json”); ostream&out=resp.send();
最可能的情况是,服务器的输出违反了cURL所容忍的一些协议规则。如果您可以拦截原始TCP连接并发布请求和响应,这将有所帮助。如果您只是需要一个快速修复,请尝试HTTP 1.0而不是1.1(在请求中)——出错的事情更少。请尝试。有同样的例外:(我如何截取原始TCP连接?这取决于你的平台。Wireshark、tcpdump和类似的工具。好的,我正在截取连接起诉Wireshark。什么样的信息可以帮助你?我不太明白那里写了什么。