0

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

1 回答 1

0

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

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

于 2012-11-13T19:54:18.230 回答