Jetty:提供消息正文总是会导致STATUS_EXCEPTED

时间:2012-11-13 17:40:01

标签: java http jetty

我们正在使用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);
}

1 个答案:

答案 0 :(得分:0)

您不能简单地将ZIP文件中的二进制字节传输到HTTP请求中。您可以考虑UUENCOD它并确保该方法是POST,因此它不会被添加到请求URL的末尾。这是一个很好的例子:

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