我在使用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 param
发送,您将获得:
414 Request-URI Too Long
答案 0 :(得分:4)
使用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
答案 1 :(得分:1)
我刚遇到一个看似相同症状的问题,但原因不同。就我而言,我试图将包含3MB数据的消息发布到servlet。增加请求缓冲区大小并不能解决问题。
问题是我重复使用相同的请求对象,而不是按实际请求创建新的请求对象。