我连接到突出返回“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)
注意相关问题无关紧要,我什至不向 API 询问响应正文,它仍然崩溃。