Subversion Repositories javautils

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * ====================================================================
  3.  *
  4.  *  Licensed to the Apache Software Foundation (ASF) under one or more
  5.  *  contributor license agreements.  See the NOTICE file distributed with
  6.  *  this work for additional information regarding copyright ownership.
  7.  *  The ASF licenses this file to You under the Apache License, Version 2.0
  8.  *  (the "License"); you may not use this file except in compliance with
  9.  *  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, software
  14.  *  distributed under the License is distributed on an "AS IS" BASIS,
  15.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16.  *  See the License for the specific language governing permissions and
  17.  *  limitations under the License.
  18.  * ====================================================================
  19.  *
  20.  * This software consists of voluntary contributions made by many
  21.  * individuals on behalf of the Apache Software Foundation.  For more
  22.  * information on the Apache Software Foundation, please see
  23.  * <http://www.apache.org/>.
  24.  */
  25.  
  26. package org.apache.http.examples.client;
  27.  
  28. import org.apache.http.HttpEntity;
  29. import org.apache.http.HttpHost;
  30. import org.apache.http.HttpResponse;
  31. import org.apache.http.auth.AuthScope;
  32. import org.apache.http.auth.UsernamePasswordCredentials;
  33. import org.apache.http.client.methods.HttpGet;
  34. import org.apache.http.conn.params.ConnRoutePNames;
  35. import org.apache.http.impl.client.DefaultHttpClient;
  36.  
  37. /**
  38.  * A simple example that uses HttpClient to execute an HTTP request
  39.  * over a secure connection tunneled through an authenticating proxy.
  40.  */
  41. public class ClientProxyAuthentication {
  42.  
  43.     public static void main(String[] args) throws Exception {
  44.        
  45.         DefaultHttpClient httpclient = new DefaultHttpClient();
  46.  
  47.         httpclient.getCredentialsProvider().setCredentials(
  48.                 new AuthScope("localhost", 8080),
  49.                 new UsernamePasswordCredentials("username", "password"));
  50.  
  51.         HttpHost targetHost = new HttpHost("www.verisign.com", 443, "https");
  52.         HttpHost proxy = new HttpHost("localhost", 8080);
  53.  
  54.         httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
  55.  
  56.         HttpGet httpget = new HttpGet("/");
  57.        
  58.         System.out.println("executing request: " + httpget.getRequestLine());
  59.         System.out.println("via proxy: " + proxy);
  60.         System.out.println("to target: " + targetHost);
  61.        
  62.         HttpResponse response = httpclient.execute(targetHost, httpget);
  63.         HttpEntity entity = response.getEntity();
  64.  
  65.         System.out.println("----------------------------------------");
  66.         System.out.println(response.getStatusLine());
  67.         if (entity != null) {
  68.             System.out.println("Response content length: " + entity.getContentLength());
  69.         }
  70.         if (entity != null) {
  71.             entity.consumeContent();
  72.         }
  73.        
  74.         // When HttpClient instance is no longer needed,
  75.         // shut down the connection manager to ensure
  76.         // immediate deallocation of all system resources
  77.         httpclient.getConnectionManager().shutdown();        
  78.     }
  79. }
  80.