【问题标题】:Jetty: Providing a message body always results in STATUS_EXCEPTEDJetty:提供消息体总是会导致 STATUS_EXCEPTED
【发布时间】:2012-11-13 17:40:00
【问题描述】:

我们正在使用 Jetty 将数据从服务器应用程序发送到同样使用 Jetty 的客户端应用程序。

由于某种原因,当设置消息内容时,响应始终为 HttpExchange.STATUS_EXCEPTED。然而,消息正在另一端接收,一切似乎都完好无损。

这是它正在构建和发送请求的服务器应用程序的代码:

HttpClient client = new HttpClient();

        try
        {
            client.start();

            ContentExchange exchange = new ContentExchange(true);
            //exchange.setMethod("POST");

            // Set the request URL
            logger.info("1");
            exchange.setURL("http://localhost:8180/network/");

            // Set the data contents type.
            logger.info("2");
            //exchange.setRequestContentType("application/zip");

            // Create a file input stream
            logger.info("3");
            FileInputStream inputFile = new FileInputStream(new File("ECF43d01.zip"));

            // Stream the file into the request
            logger.info("4");
            exchange.setRequestContentSource(inputFile); // <- This line makes the response always STATUS_EXCEPTED

            // Send the request
            logger.info("5");
            client.send(exchange);

            //logger.info(baseRequest.getMethod());

            // Waits until the exchange is terminated
            int exchangeState = exchange.waitForDone();

            if (exchangeState == HttpExchange.STATUS_COMPLETED)
                response.getWriter().println("Completed "+exchange.getResponseContent());
            else if (exchangeState == HttpExchange.STATUS_EXCEPTED)
                response.getWriter().println("Error with handler HttpExchange.STATUS_EXCEPTED");
            else if (exchangeState == HttpExchange.STATUS_EXPIRED)
                response.getWriter().println("Timeout");

这是客户端接收代码:

public void handle(String target, Request baseRequest, HttpServletRequest arg2, HttpServletResponse response) throws IOException, ServletException 
{
    Logger logger = LoggerFactory.getLogger(AbstractHandler.class);

    logger.info("Starting");
    response.setContentType("text/html;charset=utf-8");
    logger.info("Output");

    // We have handled the request. Send back a successful response
    response.setStatus(HttpServletResponse.SC_OK);
    baseRequest.setHandled(true);
}

【问题讨论】:

    标签: java http jetty


    【解决方案1】:

    您不能只是简单地将 ZIP 文件中的二进制字节通过管道传输到 HTTP 请求中。您可能会考虑对它进行 UUENCOD 编码并确保该方法是 POST,这样它就不会被附加到请求 URL 的末尾。这是一个很好的例子:

    http://wiki.developerforce.com/page/JavaClientExample.java

    【讨论】:

    • 像这样的数据打包从我所做的事情中处理得很好。数据从消息正文的另一端出来。虽然我可能应该将此添加到问题描述中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-18
    • 2012-03-15
    • 1970-01-01
    • 2016-04-13
    • 1970-01-01
    • 2020-08-13
    • 2021-10-18
    相关资源
    最近更新 更多