001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/examples/org/apache/http/examples/ElementalHttpGet.java $
003: * $Revision: 610464 $
004: * $Date: 2008-01-09 18:10:55 +0100 (Wed, 09 Jan 2008) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http.examples;
033:
034: import java.net.Socket;
035:
036: import org.apache.http.ConnectionReuseStrategy;
037: import org.apache.http.HttpHost;
038: import org.apache.http.HttpResponse;
039: import org.apache.http.HttpVersion;
040: import org.apache.http.impl.DefaultConnectionReuseStrategy;
041: import org.apache.http.impl.DefaultHttpClientConnection;
042: import org.apache.http.params.BasicHttpParams;
043: import org.apache.http.message.BasicHttpRequest;
044: import org.apache.http.params.HttpParams;
045: import org.apache.http.params.HttpProtocolParams;
046: import org.apache.http.protocol.BasicHttpProcessor;
047: import org.apache.http.protocol.HttpContext;
048: import org.apache.http.protocol.BasicHttpContext;
049: import org.apache.http.protocol.ExecutionContext;
050: import org.apache.http.protocol.HttpRequestExecutor;
051: import org.apache.http.protocol.RequestConnControl;
052: import org.apache.http.protocol.RequestContent;
053: import org.apache.http.protocol.RequestExpectContinue;
054: import org.apache.http.protocol.RequestTargetHost;
055: import org.apache.http.protocol.RequestUserAgent;
056: import org.apache.http.util.EntityUtils;
057:
058: /**
059: * Elemental example for executing a GET request.
060: *
061: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
062: *
063: *
064: * <!-- empty lines above to avoid 'svn diff' context problems -->
065: * @version $Revision: 610464 $
066: */
067: public class ElementalHttpGet {
068:
069: public static void main(String[] args) throws Exception {
070:
071: HttpParams params = new BasicHttpParams();
072: HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
073: HttpProtocolParams.setContentCharset(params, "UTF-8");
074: HttpProtocolParams.setUserAgent(params, "HttpComponents/1.1");
075: HttpProtocolParams.setUseExpectContinue(params, true);
076:
077: BasicHttpProcessor httpproc = new BasicHttpProcessor();
078: // Required protocol interceptors
079: httpproc.addInterceptor(new RequestContent());
080: httpproc.addInterceptor(new RequestTargetHost());
081: // Recommended protocol interceptors
082: httpproc.addInterceptor(new RequestConnControl());
083: httpproc.addInterceptor(new RequestUserAgent());
084: httpproc.addInterceptor(new RequestExpectContinue());
085:
086: HttpRequestExecutor httpexecutor = new HttpRequestExecutor();
087:
088: HttpContext context = new BasicHttpContext(null);
089: HttpHost host = new HttpHost("localhost", 8080);
090:
091: DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
092: ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy();
093:
094: context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
095: context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host);
096:
097: try {
098:
099: String[] targets = { "/",
100: "/servlets-examples/servlet/RequestInfoExample",
101: "/somewhere%20in%20pampa" };
102:
103: for (int i = 0; i < targets.length; i++) {
104: if (!conn.isOpen()) {
105: Socket socket = new Socket(host.getHostName(), host
106: .getPort());
107: conn.bind(socket, params);
108: }
109: BasicHttpRequest request = new BasicHttpRequest("GET",
110: targets[i]);
111: System.out.println(">> Request URI: "
112: + request.getRequestLine().getUri());
113:
114: context.setAttribute(ExecutionContext.HTTP_REQUEST,
115: request);
116: request.setParams(params);
117: httpexecutor.preProcess(request, httpproc, context);
118: HttpResponse response = httpexecutor.execute(request,
119: conn, context);
120: httpexecutor.postProcess(response, httpproc, context);
121:
122: System.out.println("<< Response: "
123: + response.getStatusLine());
124: System.out.println(EntityUtils.toString(response
125: .getEntity()));
126: System.out.println("==============");
127: if (!connStrategy.keepAlive(response, context)) {
128: conn.close();
129: } else {
130: System.out.println("Connection kept alive...");
131: }
132: }
133: } finally {
134: conn.close();
135: }
136: }
137:
138: }
|