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.util.List;
  31.  
  32. import org.apache.http.HttpEntity;
  33. import org.apache.http.HttpResponse;
  34. import org.apache.http.client.CookieStore;
  35. import org.apache.http.client.HttpClient;
  36. import org.apache.http.client.methods.HttpGet;
  37. import org.apache.http.client.protocol.ClientContext;
  38. import org.apache.http.cookie.Cookie;
  39. import org.apache.http.impl.client.BasicCookieStore;
  40. import org.apache.http.impl.client.DefaultHttpClient;
  41. import org.apache.http.protocol.HttpContext;
  42. import org.apache.http.protocol.BasicHttpContext;
  43.  
  44.  
  45. /**
  46.  * This example demonstrates the use of a local HTTP context populated with
  47.  * custom attributes.
  48.  */
  49. public class ClientCustomContext {
  50.  
  51.     public final static void main(String[] args) throws Exception {
  52.        
  53.         HttpClient httpclient = new DefaultHttpClient();
  54.  
  55.         // Create a local instance of cookie store
  56.         CookieStore cookieStore = new BasicCookieStore();
  57.        
  58.         // Create local HTTP context
  59.         HttpContext localContext = new BasicHttpContext();
  60.         // Bind custom cookie store to the local context
  61.         localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
  62.        
  63.         HttpGet httpget = new HttpGet("http://www.google.com/");
  64.  
  65.         System.out.println("executing request " + httpget.getURI());
  66.  
  67.         // Pass local context as a parameter
  68.         HttpResponse response = httpclient.execute(httpget, localContext);
  69.         HttpEntity entity = response.getEntity();
  70.  
  71.         System.out.println("----------------------------------------");
  72.         System.out.println(response.getStatusLine());
  73.         if (entity != null) {
  74.             System.out.println("Response content length: " + entity.getContentLength());
  75.         }
  76.         List<Cookie> cookies = cookieStore.getCookies();
  77.         for (int i = 0; i < cookies.size(); i++) {
  78.             System.out.println("Local cookie: " + cookies.get(i));
  79.         }
  80.        
  81.         // Consume response content
  82.         if (entity != null) {
  83.             entity.consumeContent();
  84.         }
  85.        
  86.         System.out.println("----------------------------------------");
  87.  
  88.         // When HttpClient instance is no longer needed,
  89.         // shut down the connection manager to ensure
  90.         // immediate deallocation of all system resources
  91.         httpclient.getConnectionManager().shutdown();        
  92.     }
  93.    
  94. }
  95.  
  96.