gpt4 book ai didi

java - Apache HTTP 组件大小限制响应?

转载 作者:行者123 更新时间:2023-11-29 03:37:50 26 4
gpt4 key购买 nike

我连接到 juts 返回“A”的非有限流的服务器。即:这个

$ nc 1.2.3.4 9001
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA^C

似乎 apache http 组件库只是从服务器获取完整的响应然后进行解析。如何限制大小以使 JVM 不会崩溃?

这是我的代码:

使用流畅的 API:

public static void main(String[] args) throws ClientProtocolException, IOException {
Request.Get(url).execute().discardContent();
}

使用低级 API:

public static void main(String[] args) throws ClientProtocolException, IOException {
String url = "http://1.2.3.4:9001/";
DefaultHttpClient httpclient = new DefaultHttpClient();
System.out.println("A");
HttpGet request = new HttpGet(url);
System.out.println("B");
HttpResponse hr = httpclient.execute(request);
System.out.println("C");
}

给我这个:

A
B
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at org.apache.http.util.ByteArrayBuffer.expand(ByteArrayBuffer.java:62)
at org.apache.http.util.ByteArrayBuffer.append(ByteArrayBuffer.java:92)
at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:278)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:92)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:62)
at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:254)
at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:289)
at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:252)
at org.apache.http.impl.conn.ManagedClientConnectionImpl.receiveResponseHeader(ManagedClientConnectionImpl.java:191)
at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:300)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:127)
at org.apache.http.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.java:712)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:517)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:784)
at com.dudebro.WhatHappen.main(WhatHappen.java:103)

备注related question无关紧要,我什至没有向 API 请求响应主体,它仍然崩溃。

最佳答案

您可以尝试设置以下参数:

MAX_LINE_LENGTH = "http.connection.max-line-length"
MAX_HEADER_COUNT = "http.connection.max-header-count"

我认为第一个的默认值是 -1,这意味着没有限制。

关于java - Apache HTTP 组件大小限制响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14590319/

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