Source code: org/apache/http/contrib/spring/SpringHttpDemo.java
1 /*
2 * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/tags/4.0-alpha2/src/contrib/org/apache/http/contrib/spring/SpringHttpDemo.java $
3 * $Revision: 378645 $
4 * $Date: 2006-02-17 23:34:54 +0100 (Fri, 17 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 package org.apache.http.contrib.spring;
30
31 import org.apache.http.ConnectionReuseStrategy;
32 import org.apache.http.HttpClientConnection;
33 import org.apache.http.HttpHost;
34 import org.apache.http.HttpRequest;
35 import org.apache.http.HttpRequestFactory;
36 import org.apache.http.HttpResponse;
37 import org.apache.http.HttpVersion;
38 import org.apache.http.Scheme;
39 import org.apache.http.params.HttpParams;
40 import org.apache.http.params.HttpProtocolParams;
41 import org.apache.http.protocol.HttpRequestExecutor;
42 import org.apache.http.util.EntityUtils;
43 import org.springframework.beans.factory.xml.XmlBeanFactory;
44 import org.springframework.core.io.ClassPathResource;
45
46 public class SpringHttpDemo {
47
48 public static void main(String[] args) throws Exception {
49
50 ClassPathResource res = new ClassPathResource("org/apache/http/contrib/spring/http-beans.xml");
51 XmlBeanFactory beanfactory = new XmlBeanFactory(res);
52
53 // Set global params if desired
54 HttpParams globalparams = (HttpParams) beanfactory.getBean("global-params");
55 globalparams
56 .setParameter(HttpProtocolParams.PROTOCOL_VERSION, HttpVersion.HTTP_1_1)
57 .setParameter(HttpProtocolParams.HTTP_CONTENT_CHARSET, "UTF-8")
58 .setBooleanParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, true)
59 .setParameter(HttpProtocolParams.USER_AGENT, "Jakarta-HttpComponents/1.1");
60
61 HttpParams params = (HttpParams) beanfactory.getBean("params");
62
63 HttpRequestExecutor httpexec = (HttpRequestExecutor)beanfactory.getBean("http-executor");
64 httpexec.setParams(params);
65
66 Scheme http = (Scheme) beanfactory.getBean("http-scheme");
67 HttpHost host = new HttpHost("www.yahoo.com", 80, http);
68
69 HttpRequestFactory requestfactory = (HttpRequestFactory) beanfactory.getBean("http-request-factory");
70 HttpClientConnection conn = (HttpClientConnection) beanfactory.getBean("http-connection");
71 ConnectionReuseStrategy connStrategy = (ConnectionReuseStrategy) beanfactory.getBean("conn-reuse-strategy");
72 conn.setTargetHost(host);
73 try {
74 HttpRequest request1 = requestfactory.newHttpRequest("GET", "/");
75 HttpResponse response1 = httpexec.execute(request1, conn);
76 System.out.println("<< Response: " + response1.getStatusLine());
77 System.out.println(EntityUtils.toString(response1.getEntity()));
78 System.out.println("==============");
79 if (connStrategy.keepAlive(response1)) {
80 System.out.println("Connection kept alive...");
81 } else {
82 conn.close();
83 System.out.println("Connection closed...");
84 }
85 HttpRequest request2 = requestfactory.newHttpRequest("GET", "/stuff");
86 HttpResponse response2 = httpexec.execute(request2, conn);
87 System.out.println("<< Response: " + response2.getStatusLine());
88 System.out.println(EntityUtils.toString(response2.getEntity()));
89 System.out.println("==============");
90 if (connStrategy.keepAlive(response2)) {
91 System.out.println("Connection kept alive...");
92 } else {
93 conn.close();
94 System.out.println("Connection closed...");
95 }
96 } finally {
97 conn.close();
98 }
99 }
100
101 }