001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/examples/org/apache/http/examples/ElementalHttpPost.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.io.ByteArrayInputStream;
035: import java.net.Socket;
036:
037: import org.apache.http.ConnectionReuseStrategy;
038: import org.apache.http.HttpEntity;
039: import org.apache.http.HttpHost;
040: import org.apache.http.HttpResponse;
041: import org.apache.http.HttpVersion;
042: import org.apache.http.entity.ByteArrayEntity;
043: import org.apache.http.entity.InputStreamEntity;
044: import org.apache.http.entity.StringEntity;
045: import org.apache.http.impl.DefaultConnectionReuseStrategy;
046: import org.apache.http.impl.DefaultHttpClientConnection;
047: import org.apache.http.params.BasicHttpParams;
048: import org.apache.http.message.BasicHttpEntityEnclosingRequest;
049: import org.apache.http.params.HttpParams;
050: import org.apache.http.params.HttpProtocolParams;
051: import org.apache.http.protocol.BasicHttpProcessor;
052: import org.apache.http.protocol.HttpContext;
053: import org.apache.http.protocol.BasicHttpContext;
054: import org.apache.http.protocol.ExecutionContext;
055: import org.apache.http.protocol.HttpRequestExecutor;
056: import org.apache.http.protocol.RequestConnControl;
057: import org.apache.http.protocol.RequestContent;
058: import org.apache.http.protocol.RequestExpectContinue;
059: import org.apache.http.protocol.RequestTargetHost;
060: import org.apache.http.protocol.RequestUserAgent;
061: import org.apache.http.util.EntityUtils;
062:
063: /**
064: * Elemental example for executing a POST request.
065: *
066: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
067: *
068: *
069: * <!-- empty lines above to avoid 'svn diff' context problems -->
070: * @version $Revision: 610464 $
071: */
072: public class ElementalHttpPost {
073:
074: public static void main(String[] args) throws Exception {
075:
076: HttpParams params = new BasicHttpParams();
077: HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
078: HttpProtocolParams.setContentCharset(params, "UTF-8");
079: HttpProtocolParams.setUserAgent(params, "HttpComponents/1.1");
080: HttpProtocolParams.setUseExpectContinue(params, true);
081:
082: BasicHttpProcessor httpproc = new BasicHttpProcessor();
083: // Required protocol interceptors
084: httpproc.addInterceptor(new RequestContent());
085: httpproc.addInterceptor(new RequestTargetHost());
086: // Recommended protocol interceptors
087: httpproc.addInterceptor(new RequestConnControl());
088: httpproc.addInterceptor(new RequestUserAgent());
089: httpproc.addInterceptor(new RequestExpectContinue());
090:
091: HttpRequestExecutor httpexecutor = new HttpRequestExecutor();
092:
093: HttpContext context = new BasicHttpContext(null);
094:
095: HttpHost host = new HttpHost("localhost", 8080);
096:
097: DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
098: ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy();
099:
100: context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
101: context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host);
102:
103: try {
104:
105: HttpEntity[] requestBodies = {
106: new StringEntity("This is the first test request",
107: "UTF-8"),
108: new ByteArrayEntity(
109: "This is the second test request"
110: .getBytes("UTF-8")),
111: new InputStreamEntity(new ByteArrayInputStream(
112: "This is the third test request (will be chunked)"
113: .getBytes("UTF-8")), -1) };
114:
115: for (int i = 0; i < requestBodies.length; i++) {
116: if (!conn.isOpen()) {
117: Socket socket = new Socket(host.getHostName(), host
118: .getPort());
119: conn.bind(socket, params);
120: }
121: BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest(
122: "POST",
123: "/servlets-examples/servlet/RequestInfoExample");
124: request.setEntity(requestBodies[i]);
125: System.out.println(">> Request URI: "
126: + request.getRequestLine().getUri());
127:
128: context.setAttribute(ExecutionContext.HTTP_REQUEST,
129: request);
130: request.setParams(params);
131: httpexecutor.preProcess(request, httpproc, context);
132: HttpResponse response = httpexecutor.execute(request,
133: conn, context);
134: httpexecutor.postProcess(response, httpproc, context);
135:
136: System.out.println("<< Response: "
137: + response.getStatusLine());
138: System.out.println(EntityUtils.toString(response
139: .getEntity()));
140: System.out.println("==============");
141: if (!connStrategy.keepAlive(response, context)) {
142: conn.close();
143: } else {
144: System.out.println("Connection kept alive...");
145: }
146: }
147: } finally {
148: conn.close();
149: }
150: }
151:
152: }
|