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