Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何限制HttpClient响应长度_Java_Httpclient_Htmlunit - Fatal编程技术网

Java 如何限制HttpClient响应长度

Java 如何限制HttpClient响应长度,java,httpclient,htmlunit,Java,Httpclient,Htmlunit,我正在将htmlunit与httpclient一起使用。如何将httpclient的响应正文长度限制为1MB?技巧很简单。您必须获取InputStream,从中读取,并在超出限制时停止读取 InputStream instream = method.getResponseBodyAsStream(); 我已经对apache示例进行了一些调优 import java.io.ByteArrayOutputStream; import java.io.IOException; impor

我正在将
htmlunit
httpclient
一起使用。如何将
httpclient
响应正文长度限制为
1MB

技巧很简单。您必须获取InputStream,从中读取,并在超出限制时停止读取

      InputStream instream = method.getResponseBodyAsStream();
我已经对apache示例进行了一些调优

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
public class HttpClientTutorial {

  private static String url = "http://www.apache.com";
  private static final int LIMIT = 1024*1024;//set to 1MB
  public static void main(String[] args) {
    // Create an instance of HttpClient.
    HttpClient client = new HttpClient();
    // Create a method instance.
    GetMethod method = new GetMethod(url);   
    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, 
            new DefaultHttpMethodRetryHandler(3, false));
    try {
      // Execute the method.
      int statusCode = client.executeMethod(method);
      if (statusCode != HttpStatus.SC_OK) {
        System.err.println("Method failed: " + method.getStatusLine());
      }
      // Read the response body.
      //byte[] responseBody = method.getResponseBody();
      byte[] responseBody = null;    
      InputStream instream = method.getResponseBodyAsStream();
      if (instream != null) {
          long contentLength = method.getResponseContentLength();
          if (contentLength < Integer.MAX_VALUE) { //guard below cast from overflow
              ByteArrayOutputStream outstream = new ByteArrayOutputStream();
              byte[] buffer = new byte[1024];
              int len;
              int total = 0;
              while ((len = instream.read(buffer)) > 0 && total<LIMIT) {
                  outstream.write(buffer, 0, len);
                  total+= len;
              }
              responseBody = outstream.toByteArray();
              outstream.close();
              instream.close();
              System.out.println(new String(responseBody));
          }
      }
    } catch (HttpException e) {
      System.err.println("Fatal protocol violation: " + e.getMessage());
      e.printStackTrace();
    } catch (IOException e) {
      System.err.println("Fatal transport error: " + e.getMessage());
      e.printStackTrace();
    } finally {
      // Release the connection.
      method.releaseConnection();
    }  
  }
}
import java.io.ByteArrayOutputStream;
导入java.io.IOException;
导入java.io.InputStream;
导入org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
导入org.apache.commons.httpclient.httpclient;
导入org.apache.commons.httpclient.HttpException;
导入org.apache.commons.httpclient.HttpStatus;
导入org.apache.commons.httpclient.methods.GetMethod;
导入org.apache.commons.httpclient.params.HttpMethodParams;
公共类HttpClientTutorial{
专用静态字符串url=”http://www.apache.com";
私有静态最终整数限制=1024*1024;//设置为1MB
公共静态void main(字符串[]args){
//创建HttpClient的实例。
HttpClient=新的HttpClient();
//创建一个方法实例。
GetMethod=新的GetMethod(url);
//需要提供自定义重试处理程序
方法.getParams().setParameter(HttpMethodParams.RETRY_处理程序,
新的DefaultHttpMethodRetryHandler(3,false));
试一试{
//执行该方法。
int statusCode=client.executeMethod(方法);
if(statusCode!=HttpStatus.SC\u OK){
System.err.println(“方法失败:+Method.getStatusLine());
}
//阅读回复正文。
//byte[]responseBody=method.getResponseBody();
字节[]responseByte=null;
InputStream instream=method.getResponseBodyAsStream();
如果(流内!=null){
long contentLength=method.getResponseContentLength();
if(contentLength0&&total可以从ApacheCommons使用。

这是一个只提供特定长度字节的流-如果其位置超过该长度,它将停止。

您可以扩展您试图实现的内容吗?为什么只有1mb?