Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/apache/http/examples/ElementalHttpGet.java


1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/tags/4.0-alpha2/src/examples/org/apache/http/examples/ElementalHttpGet.java $
3    * $Revision: 376458 $
4    * $Date: 2006-02-09 23:22:06 +0100 (Thu, 09 Feb 2006) $
5    *
6    * ====================================================================
7    *
8    *  Copyright 1999-2006 The Apache Software Foundation
9    *
10   *  Licensed under the Apache License, Version 2.0 (the "License");
11   *  you may not use this file except in compliance with the License.
12   *  You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   * ====================================================================
22   *
23   * This software consists of voluntary contributions made by many
24   * individuals on behalf of the Apache Software Foundation.  For more
25   * information on the Apache Software Foundation, please see
26   * <http://www.apache.org/>.
27   *
28   */
29  
30  package org.apache.http.examples;
31  
32  import org.apache.http.ConnectionReuseStrategy;
33  import org.apache.http.HttpClientConnection;
34  import org.apache.http.HttpHost;
35  import org.apache.http.HttpResponse;
36  import org.apache.http.HttpVersion;
37  import org.apache.http.Scheme;
38  import org.apache.http.impl.DefaultConnectionReuseStrategy;
39  import org.apache.http.impl.DefaultHttpClientConnection;
40  import org.apache.http.impl.DefaultHttpParams;
41  import org.apache.http.impl.io.PlainSocketFactory;
42  import org.apache.http.io.SocketFactory;
43  import org.apache.http.message.HttpGet;
44  import org.apache.http.params.HttpParams;
45  import org.apache.http.params.HttpProtocolParams;
46  import org.apache.http.protocol.HttpRequestExecutor;
47  import org.apache.http.protocol.RequestConnControl;
48  import org.apache.http.protocol.RequestContent;
49  import org.apache.http.protocol.RequestExpectContinue;
50  import org.apache.http.protocol.RequestTargetHost;
51  import org.apache.http.protocol.RequestUserAgent;
52  import org.apache.http.util.EntityUtils;
53  
54  /**
55   * <p>
56   * </p>
57   * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
58   *
59   * @version $Revision: 376458 $
60   */
61  public class ElementalHttpGet {
62  
63      public static void main(String[] args) throws Exception {
64          
65          SocketFactory socketfactory = PlainSocketFactory.getSocketFactory();
66          Scheme.registerScheme("http", new Scheme("http", socketfactory, 80));
67  
68          HttpParams params = new DefaultHttpParams(null);
69          HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
70          HttpProtocolParams.setContentCharset(params, "UTF-8");
71          HttpProtocolParams.setUserAgent(params, "Jakarta-HttpComponents/1.1");
72          HttpProtocolParams.setUseExpectContinue(params, true);
73          
74          HttpRequestExecutor httpexecutor = new HttpRequestExecutor();
75          httpexecutor.setParams(params);
76          // Required protocol interceptors
77          httpexecutor.addInterceptor(new RequestContent());
78          httpexecutor.addInterceptor(new RequestTargetHost());
79          // Recommended protocol interceptors
80          httpexecutor.addInterceptor(new RequestConnControl());
81          httpexecutor.addInterceptor(new RequestUserAgent());
82          httpexecutor.addInterceptor(new RequestExpectContinue());
83          
84          HttpHost host = new HttpHost("localhost", 8080);
85          HttpClientConnection conn = new DefaultHttpClientConnection(host);
86          try {
87              
88              String[] targets = {
89                      "/",
90                      "/servlets-examples/servlet/RequestInfoExample", 
91                      "/somewhere%20in%20pampa"};
92              
93              ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy();
94              
95              for (int i = 0; i < targets.length; i++) {
96                  HttpGet request = new HttpGet(targets[i]);
97                  System.out.println(">> Request URI: " + request.getRequestLine().getUri());
98                  HttpResponse response = httpexecutor.execute(request, conn);
99                  System.out.println("<< Response: " + response.getStatusLine());
100                 System.out.println(EntityUtils.toString(response.getEntity()));
101                 System.out.println("==============");
102                 if (!connStrategy.keepAlive(response)) {
103                     conn.close();
104                 } else {
105                     System.out.println("Connection kept alive...");
106                 }
107             }
108         } finally {
109             conn.close();
110         }
111     }
112     
113 }