public class SpringHttpDemo {
public static void main(String[] args) throws Exception {
ClassPathResource res = new ClassPathResource("org/apache/http/contrib/spring/http-beans.xml");
XmlBeanFactory beanfactory = new XmlBeanFactory(res);
// Set global params if desired
HttpParams globalparams = (HttpParams) beanfactory.getBean("global-params");
globalparams
.setParameter(HttpProtocolParams.PROTOCOL_VERSION, HttpVersion.HTTP_1_1)
.setParameter(HttpProtocolParams.HTTP_CONTENT_CHARSET, "UTF-8")
.setBooleanParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, true)
.setParameter(HttpProtocolParams.USER_AGENT, "Jakarta-HttpComponents/1.1");
HttpParams params = (HttpParams) beanfactory.getBean("params");
HttpRequestExecutor httpexec = (HttpRequestExecutor)beanfactory.getBean("http-executor");
httpexec.setParams(params);
Scheme http = (Scheme) beanfactory.getBean("http-scheme");
HttpHost host = new HttpHost("www.yahoo.com", 80, http);
HttpRequestFactory requestfactory = (HttpRequestFactory) beanfactory.getBean("http-request-factory");
HttpClientConnection conn = (HttpClientConnection) beanfactory.getBean("http-connection");
ConnectionReuseStrategy connStrategy = (ConnectionReuseStrategy) beanfactory.getBean("conn-reuse-strategy");
conn.setTargetHost(host);
try {
HttpRequest request1 = requestfactory.newHttpRequest("GET", "/");
HttpResponse response1 = httpexec.execute(request1, conn);
System.out.println("<< Response: " + response1.getStatusLine());