响应标题太大-Jetty Embedded版本9 [英] Response Header too large - Jetty Embedded version 9

查看:235
本文介绍了响应标题太大-Jetty Embedded版本9的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Jetty时,出现响应头太大"的异常,该异常仅在jsonValue的大小较大(大于1500个字节)时才在客户端抛出.如果jsonvalue较小,则一切正常.

I'm getting an exception of "Response Header too large" when using Jetty, the exception is thrown at the Client and only when the size of the jsonValue is large (greater than 1500 bytes). If the jsonvalue is smaller, everything works fine.

这是我的代码,非常简单.

This is the code I have, it is very simple.

服务器代码:

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

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

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

StoreServlet类代码:

StoreServlet class code:

@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:

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

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

The answer selected was perfect, I just want to add that when sending a large file it is better to use this method:

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

否则,如果将大型jsonValue作为POST param发送,您将得到:

Otherwise, if sending a large jsonValue as a POST param, you will get:

414 Request-URI Too Long

推荐答案

使用Maven码头插件版本:9.1.3.v20140225

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

You are going to need a custom value for your "request buffer size". By default it is 4096 bytes.

您可以使用HttpClient类的setRequestBufferSize方法(请参阅javadoc:

You can use the method setRequestBufferSize of the HttpClient class (see 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客户端实现发送.例如,对于我来说,并使用上面的代码,我将发送以下内容:

Your custom value will depend on how many bytes you are sending plus a few more, which are always sent by the HTTP client implementation. For example, in my case and using the above code I am sending the following:

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

这篇关于响应标题太大-Jetty Embedded版本9的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆