开发者社区> 问答> 正文

httpclient如何实现一次连接发送多个请求?

如题,就是要实现在第一个请求发出之后建立起了连接,那么在该线程中以后的所有请求都使用该连接来进行通信,我理解的同一个连接就是建立连接以后再次发送请求仍然使用上一次的端口号,保证本地端口号和远端端口号的唯一映射(相当于TCP),以后的请求不用new以致产生新的端口映射(大批量请求在每次都new的话会产生异常)!谢谢!最好能给个简单的demo

展开
收起
蛮大人123 2016-03-10 10:35:56 4972 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪

    This is an example of an Apache HttpClient 4.3 pool of connections which do not require authentication:

    public class PoolOfHttpConnections{
       static String[] urisToGet = {"http://www.site1.com", "http://www.site2.com"};
    
        public static void main(String[] args) throws Exception {
               CloseableHttpClient httpclient = HttpClients.createDefault();
               // create a thread for each link
               GetThread[] threads = new GetThread[urisToGet.length];
               for (int i = 0; i < threads.length; i++) {
                   HttpGet httpget = new HttpGet(urisToGet[i]);
                   threads[i] = new GetThread(httpClient, httpget);
               }
    
               // start the threads
               for (int j = 0; j < threads.length; j++) {
                   threads[j].start();
               }
               // join the threads
               for (int j = 0; j < threads.length; j++) {
                   threads[j].join();
               }
        } //end main
    
        private static class GetThread extends Thread {
    
                private final CloseableHttpClient httpClient;
                private final HttpContext context;
                private final HttpGet httpget;
    
                public GetThread(CloseableHttpClient httpClient, HttpGet httpget) {
                       this.httpClient = httpClient;
                       this.context = HttpClientContext.create();
                       this.httpget = httpget;
                }
    
                @Override
                public void run() {
                       try {
                           CloseableHttpResponse response = httpClient.execute(httpget, context);
                           try {
                               HttpEntity entity = response.getEntity();
                               System.out.println("----------------------------------------");
                               Date date = new Date();
                               System.out.println("Beginning*******************");
                               System.out.println(date.toString());
                               System.out.println("There are "+urisToGet.length+" threads running in parallel!");
                               System.out.println(response.getStatusLine());
                               if (entity != null) {
                                  System.out.println("Response content length: " + entity.getContentLength());
                               }
                               System.out.println(EntityUtils.toString(entity));
                               EntityUtils.consume(entity);
                           } finally {
                             response.close();
                             System.out.println("End*******************");
                           }
                       } catch (ClientProtocolException ex) {
                              // Handle protocol errors
                       } catch (IOException ex) {
                              // Handle I/O errors
                       }
                }
        } /*end private class*/  }//end public class PoolOfHttpConnections
    2019-07-17 18:57:04
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
探索连接的最后十秒”落时”的网关 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载