Subversion Repositories javautils

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * ====================================================================
  3.  * Licensed to the Apache Software Foundation (ASF) under one
  4.  * or more contributor license agreements.  See the NOTICE file
  5.  * distributed with this work for additional information
  6.  * regarding copyright ownership.  The ASF licenses this file
  7.  * to you under the Apache License, Version 2.0 (the
  8.  * "License"); you may not use this file except in compliance
  9.  * with the License.  You may obtain a copy of the License at
  10.  *
  11.  *   http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing,
  14.  * software distributed under the License is distributed on an
  15.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16.  * KIND, either express or implied.  See the License for the
  17.  * specific language governing permissions and limitations
  18.  * under the License.
  19.  * ====================================================================
  20.  *
  21.  * This software consists of voluntary contributions made by many
  22.  * individuals on behalf of the Apache Software Foundation.  For more
  23.  * information on the Apache Software Foundation, please see
  24.  * <http://www.apache.org/>.
  25.  *
  26.  */
  27.  
  28. package org.apache.http.examples.client;
  29.  
  30. import java.io.BufferedReader;
  31. import java.io.IOException;
  32. import java.io.InputStreamReader;
  33.  
  34. import org.apache.http.HttpEntity;
  35. import org.apache.http.HttpResponse;
  36. import org.apache.http.client.HttpClient;
  37. import org.apache.http.client.methods.HttpGet;
  38. import org.apache.http.impl.client.DefaultHttpClient;
  39.  
  40. /**
  41.  * This example demonstrates the recommended way of using API to make sure
  42.  * the underlying connection gets released back to the connection manager.
  43.  */
  44. public class ClientConnectionRelease {
  45.  
  46.     public final static void main(String[] args) throws Exception {
  47.         HttpClient httpclient = new DefaultHttpClient();
  48.  
  49.         HttpGet httpget = new HttpGet("http://www.apache.org/");
  50.  
  51.         // Execute HTTP request
  52.         System.out.println("executing request " + httpget.getURI());
  53.         HttpResponse response = httpclient.execute(httpget);
  54.  
  55.         System.out.println("----------------------------------------");
  56.         System.out.println(response.getStatusLine());
  57.         System.out.println("----------------------------------------");
  58.  
  59.         // Get hold of the response entity
  60.         HttpEntity entity = response.getEntity();
  61.        
  62.         // If the response does not enclose an entity, there is no need
  63.         // to bother about connection release
  64.         if (entity != null) {
  65.             BufferedReader reader = new BufferedReader(
  66.                     new InputStreamReader(entity.getContent()));
  67.             try {
  68.                
  69.                 // do something useful with the response
  70.                 System.out.println(reader.readLine());
  71.                
  72.             } catch (IOException ex) {
  73.  
  74.                 // In case of an IOException the connection will be released
  75.                 // back to the connection manager automatically
  76.                 throw ex;
  77.                
  78.             } catch (RuntimeException ex) {
  79.  
  80.                 // In case of an unexpected exception you may want to abort
  81.                 // the HTTP request in order to shut down the underlying
  82.                 // connection and release it back to the connection manager.
  83.                 httpget.abort();
  84.                 throw ex;
  85.                
  86.             } finally {
  87.  
  88.                 // Closing the input stream will trigger connection release
  89.                 reader.close();
  90.                
  91.             }
  92.         }
  93.  
  94.         // When HttpClient instance is no longer needed,
  95.         // shut down the connection manager to ensure
  96.         // immediate deallocation of all system resources
  97.         httpclient.getConnectionManager().shutdown();        
  98.     }
  99.  
  100. }
  101.  
  102.