gpt4 book ai didi

java - 响应 header 太大 - Jetty Embedded 版本 9

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:47:24 25 4
gpt4 key购买 nike

我在使用 Jetty 时收到“响应 header 太大”的异常,该异常在客户端抛出并且仅当 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();

统计跟踪:

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

最佳答案

使用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

关于java - 响应 header 太大 - Jetty Embedded 版本 9,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22471808/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com