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 org.apache.http.Header;
  31. import org.apache.http.HttpEntity;
  32. import org.apache.http.HttpHost;
  33. import org.apache.http.HttpResponse;
  34. import org.apache.http.HttpVersion;
  35. import org.apache.http.client.HttpClient;
  36. import org.apache.http.client.methods.HttpGet;
  37. import org.apache.http.conn.ClientConnectionManager;
  38. import org.apache.http.conn.scheme.PlainSocketFactory;
  39. import org.apache.http.conn.scheme.Scheme;
  40. import org.apache.http.conn.scheme.SchemeRegistry;
  41. import org.apache.http.impl.client.DefaultHttpClient;
  42. import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
  43. import org.apache.http.params.BasicHttpParams;
  44. import org.apache.http.params.HttpParams;
  45. import org.apache.http.params.HttpProtocolParams;
  46. import org.apache.http.util.EntityUtils;
  47.  
  48. /**
  49.  * How to send a request directly using {@link HttpClient}.
  50.  *
  51.  * @since 4.0
  52.  */
  53. public class ClientExecuteDirect {
  54.  
  55.     public static void main(String[] args) throws Exception {
  56.  
  57.         HttpHost target = new HttpHost("www.apache.org", 80, "http");
  58.  
  59.         // general setup
  60.         SchemeRegistry supportedSchemes = new SchemeRegistry();
  61.  
  62.         // Register the "http" protocol scheme, it is required
  63.         // by the default operator to look up socket factories.
  64.         supportedSchemes.register(new Scheme("http",
  65.                 PlainSocketFactory.getSocketFactory(), 80));
  66.  
  67.         // prepare parameters
  68.         HttpParams params = new BasicHttpParams();
  69.         HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
  70.         HttpProtocolParams.setContentCharset(params, "UTF-8");
  71.         HttpProtocolParams.setUseExpectContinue(params, true);
  72.  
  73.         ClientConnectionManager connMgr = new ThreadSafeClientConnManager(params,
  74.                 supportedSchemes);
  75.         DefaultHttpClient httpclient = new DefaultHttpClient(connMgr, params);
  76.  
  77.         HttpGet req = new HttpGet("/");
  78.  
  79.         System.out.println("executing request to " + target);
  80.  
  81.         HttpResponse rsp = httpclient.execute(target, req);
  82.         HttpEntity entity = rsp.getEntity();
  83.  
  84.         System.out.println("----------------------------------------");
  85.         System.out.println(rsp.getStatusLine());
  86.         Header[] headers = rsp.getAllHeaders();
  87.         for (int i = 0; i < headers.length; i++) {
  88.             System.out.println(headers[i]);
  89.         }
  90.         System.out.println("----------------------------------------");
  91.  
  92.         if (entity != null) {
  93.             System.out.println(EntityUtils.toString(entity));
  94.         }
  95.  
  96.         // When HttpClient instance is no longer needed,
  97.         // shut down the connection manager to ensure
  98.         // immediate deallocation of all system resources
  99.         httpclient.getConnectionManager().shutdown();        
  100.     }
  101.  
  102. }
  103.