Apache HttpAsyncClient读取响应?

时间:2018-04-23 10:44:27

标签: java apache http apache-httpasyncclient

 private void init() {
   #Reactor 
   ioReactorConfig = IOReactorConfig.custom()                    
          .setIoThreadCount(Runtime.getRuntime().availableProcessors())
                            .setConnectTimeout(30000)
                            .setSoTimeout(30000)
                            .build();

   try {
       ioReactor = new DefaultConnectingIOReactor(ioReactorConfig);
   } catch (IOReactorException e) {
       e.printStackTrace();
       //TODO handle exception
    }
    connManager = new PoolingNHttpClientConnectionManager(ioReactor);
    httpClient = HttpAsyncClients.custom().setConnectionManager(connManager).build();

}

  private  ZCResponse httPost(URI uri, Object object,List<NameValuePair> params, Map<String,String> headers) {
        HttpPost postRequest = new HttpPost(uri);
        HttpResponse httpResponse = null;
        try {
            addHeaders(postRequest,headers);
            addPostParams(postRequest,object,params);
            Future<HttpResponse> futureResponse = httpClient.execute(postRequest, null);
            httpResponse = futureResponse.get();
            response = **readResponse(httpResponse);**
   }

private  String readResponse(HttpResponse response) throws IOException {
            BufferedReader rd = new BufferedReader(
                    new InputStreamReader(response.getEntity().getContent()));

            StringBuffer result = new StringBuffer();
            String line = "";
            while ((line = rd.readLine()) != null) {
                result.append(line);
            }
            rd.close();
            return result.toString();
        }

我对使用Apache Http Async客户端的代码有以下疑问

  1. reactor与NPoolingConnectionManager的作用是什么。
  2. 目前,响应主体是从发布请求的流中读取的。并且不使用NIO或非阻塞方式读取响应正文。这是正确的方法。

0 个答案:

没有答案