java – 响应标头太大 – Jetty嵌入式版本9

我在使用Jetty时遇到“响应标头太大”的异常,只有当jsonValue的大小很大(大于1500字节)时才会抛出异常.如果jsonvalue较小,一切正常.

这是我的代码,很简单.

服务器代码:

Server server = new Server(8080);
ServletHandler handler = new ServletHandler();
server.setHandler(handler);

handler.addServletWithMapping(StoreServlet.class, "/store");

server.start();
server.join();

StoreServlet类代码:

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
    response.setStatus(HttpServletResponse.SC_OK);
    response.getWriter().println("<h1>Store SimpleServlet</h1>");
}

客户代码:

HttpClient httpClient = new HttpClient();
httpClient.start();

ContentResponse response = httpClient.POST("http://0.0.0.0:" + host.getPort() + "/store").param("p", jsonValue).send();

StactTrace:

java.util.concurrent.ExecutionException: java.io.IOException: Response header too large
    at org.eclipse.jetty.client.util.FutureResponseListener.getResult(FutureResponseListener.java:118)
    at org.eclipse.jetty.client.util.FutureResponseListener.get(FutureResponseListener.java:101)
    at org.eclipse.jetty.client.HttpRequest.send(HttpRequest.java:589)
    at com.paywithbytes.communication.http.client.HttpClientHelper.doPost(HttpClientHelper.java:54)
    at com.paywithbytes.communication.http.client.HttpClientHelper.main(HttpClientHelper.java:35)
Caused by: java.io.IOException: Response header too large
    at org.eclipse.jetty.http.HttpGenerator.generateRequest(HttpGenerator.java:249)
    at org.eclipse.jetty.client.http.HttpSenderOverHTTP.sendHeaders(HttpSenderOverHTTP.java:78)
    at org.eclipse.jetty.client.HttpSender.send(HttpSender.java:191)
    at org.eclipse.jetty.client.http.HttpChannelOverHTTP.send(HttpChannelOverHTTP.java:52)
    at org.eclipse.jetty.client.http.HttpConnectionOverHTTP$Delegate.send(HttpConnectionOverHTTP.java:168)
    at org.eclipse.jetty.client.http.HttpConnectionOverHTTP.send(HttpConnectionOverHTTP.java:69)
    at org.eclipse.jetty.client.http.HttpDestinationOverHTTP.send(HttpDestinationOverHTTP.java:36)
    at org.eclipse.jetty.client.http.HttpDestinationOverHTTP.send(HttpDestinationOverHTTP.java:26)
    at org.eclipse.jetty.client.PoolingHttpDestination$2.run(PoolingHttpDestination.java:130)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:607)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:536)
    at java.lang.Thread.run(Thread.java:724)
Caused by: java.nio.BufferOverflowException
    at java.nio.HeapByteBuffer.put(HeapByteBuffer.java:183)
    at java.nio.ByteBuffer.put(ByteBuffer.java:832)
    at org.eclipse.jetty.http.HttpGenerator.generateRequestLine(HttpGenerator.java:500)
    at org.eclipse.jetty.http.HttpGenerator.generateRequest(HttpGenerator.java:218)
    ... 11 more

选择的答案是完美的,我只想补充一点,发送大文件时最好使用这种方法:

Request request = httpClient.POST("http://0.0.0.0:" + host.getPort() + "/store").content(
            new BytesContentProvider(jsonValue.getBytes()), "text/plain");

否则,如果将大型jsonValue作为POST参数发送,您将获得:

414 Request-URI Too Long

解决方法:

使用maven jetty插件版本:9.1.3.v20140225

您将需要“请求缓冲区大小”的自定义值.默认情况下,它是4096字节.

您可以使用HttpClient类的方法setRequestBufferSize(请参阅javadoc:org.eclipse.jetty.client.HttpClient)

HttpClient httpClient = new HttpClient();
httpClient.setRequestBufferSize(/*your custom value*/);
httpClient.start();

ContentResponse response = httpClient.POST("http://0.0.0.0:" + host.getPort() + "/store").param("p", jsonValue).send();

您的自定义值将取决于您发送的字节数加上一些,这些字节始终由HTTP客户端实现发送.例如,在我的情况下并使用上面的代码我发送以下内容:

POST /?p=YOUR_JSON_VALUE HTTP/1.1
Accept-Encoding: gzip
Host: 0.0.0.0:8080/store
User-Agent: Jetty/9.1.3.v20140225
上一篇:Maven部署:找不到Java类java.lang.String(…)的消息体编写器


下一篇:java – 将Jetty设置为服务