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.  * [Additional notices, if required by prior licensing conditions]
  26.  *
  27.  */
  28.  
  29. package org.apache.http.examples.client;
  30.  
  31. import org.apache.http.HttpEntity;
  32. import org.apache.http.HttpResponse;
  33. import org.apache.http.auth.AuthScope;
  34. import org.apache.http.auth.UsernamePasswordCredentials;
  35. import org.apache.http.client.methods.HttpGet;
  36. import org.apache.http.impl.client.DefaultHttpClient;
  37.  
  38. /**
  39.  * A simple example that uses HttpClient to execute an HTTP request against
  40.  * a target site that requires user authentication.
  41.  */
  42. public class ClientAuthentication {
  43.  
  44.     public static void main(String[] args) throws Exception {
  45.         DefaultHttpClient httpclient = new DefaultHttpClient();
  46.  
  47.         httpclient.getCredentialsProvider().setCredentials(
  48.                 new AuthScope("localhost", 443),
  49.                 new UsernamePasswordCredentials("username", "password"));
  50.        
  51.         HttpGet httpget = new HttpGet("https://localhost/protected");
  52.        
  53.         System.out.println("executing request" + httpget.getRequestLine());
  54.         HttpResponse response = httpclient.execute(httpget);
  55.         HttpEntity entity = response.getEntity();
  56.  
  57.         System.out.println("----------------------------------------");
  58.         System.out.println(response.getStatusLine());
  59.         if (entity != null) {
  60.             System.out.println("Response content length: " + entity.getContentLength());
  61.         }
  62.         if (entity != null) {
  63.             entity.consumeContent();
  64.         }
  65.  
  66.         // When HttpClient instance is no longer needed,
  67.         // shut down the connection manager to ensure
  68.         // immediate deallocation of all system resources
  69.         httpclient.getConnectionManager().shutdown();        
  70.     }
  71. }
  72.