簡體   English   中英

Poco :: HttpClientSession.receiveResponse()拋出NoMessageException沒有任何明顯的原因

[英]Poco::HttpClientSession.receiveResponse() throws NoMessageException without any apparent reason

我用Poco用Java編寫了HTTP服務器,並用C ++編寫了客戶端。 這是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);

在最后一行,我得到以下錯誤:

terminate called after throwing an instance of 'Poco::Net::NoMessageException'
  what():  No message received

但是我不明白為什么。 連接成功建立,請求的頁面存在。 我在已知的網站(例如Wikipedia)上嘗試了相同的代碼,並且沒有任何異常。

我還嘗試在命令行中使用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) {}
    }
}

即使使用此服務器,我也會遇到同樣的異常。 那這怎么了?

根據實驗,我發現如果要發出空的POST請求,則必須包含Content-Length標頭。

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

我必須在處理程序代碼中使用以下序列,以使收到的響應最少,而不會收到“ 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM