updated examples and a minor bug fix

git-svn-id: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpclient/trunk@505656 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Roland Weber 2007-02-10 08:28:36 +00:00
parent 7d2d2f47a8
commit b30a062220
7 changed files with 212 additions and 12 deletions

View File

@ -60,17 +60,17 @@ import org.apache.http.impl.client.DefaultHttpClient;
/**
* How to send a request using {@link HttpClient HttpClient}.
* How to send a request directly using {@link HttpClient HttpClient}.
*
* @author <a href="mailto:rolandw at apache.org">Roland Weber</a>
*
*
* <!-- empty lines above to avoid 'svn diff' context problems -->
* @version $Revision$ $Date$
* @version $Revision$
*
* @since 4.0
*/
public class ClientExecuteRequest {
public class ClientExecuteDirect {
/**
* The default parameters.
@ -149,9 +149,7 @@ public class ClientExecuteRequest {
SocketFactory sf = PlainSocketFactory.getSocketFactory();
SchemeRegistry.DEFAULT.register(new Scheme("http", sf, 80));
// Prepare parameters.
// Since this example doesn't use the full core framework,
// only few parameters are actually required.
// prepare parameters
HttpParams params = new DefaultHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, "UTF-8");
@ -187,5 +185,5 @@ public class ClientExecuteRequest {
}
} // class ManagerConnectDirect
} // class ClientExecuteDirect

View File

@ -0,0 +1,202 @@
/*
* $HeadURL$
* $Revision$
* $Date$
*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.http.examples.client;
import org.apache.http.HttpHost;
import org.apache.http.Header;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.message.BasicHttpRequest;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.impl.params.DefaultHttpParams;
import org.apache.http.protocol.BasicHttpProcessor;
import org.apache.http.protocol.RequestConnControl;
import org.apache.http.protocol.RequestContent;
import org.apache.http.protocol.RequestExpectContinue;
import org.apache.http.protocol.RequestTargetHost;
import org.apache.http.protocol.RequestUserAgent;
import org.apache.http.conn.Scheme;
import org.apache.http.conn.SchemeRegistry;
import org.apache.http.conn.SocketFactory;
import org.apache.http.conn.PlainSocketFactory;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.HostConfiguration;
import org.apache.http.impl.conn.ThreadSafeClientConnManager;
import org.apache.http.client.HttpClient;
import org.apache.http.client.RoutedRequest;
import org.apache.http.impl.client.DefaultHttpClient;
/**
* How to send a request via proxy using {@link HttpClient HttpClient}.
*
* @author <a href="mailto:rolandw at apache.org">Roland Weber</a>
*
*
* <!-- empty lines above to avoid 'svn diff' context problems -->
* @version $Revision$
*
* @since 4.0
*/
public class ClientExecuteProxy {
/**
* The default parameters.
* Instantiated in {@link #setup setup}.
*/
private static HttpParams defaultParameters = null;
/**
* Main entry point to this example.
*
* @param args ignored
*/
public final static void main(String[] args)
throws Exception {
// make sure to use a proxy that supports CONNECT
final HttpHost target =
new HttpHost("issues.apache.org", 443, "https");
final HttpHost proxy =
new HttpHost("127.0.0.1", 8666, "http");
setup(); // some general setup
HttpClient client = createHttpClient();
HttpRequest req = createRequest(target);
final HostConfiguration config =
new HostConfiguration(target, proxy, null);
final RoutedRequest roureq = new RoutedRequest.Impl(req, config);
System.out.println("executing request to " + target + " via " + proxy);
try {
HttpResponse rsp = client.execute(roureq, null);
System.out.println("----------------------------------------");
System.out.println(rsp.getStatusLine());
Header[] headers = rsp.getAllHeaders();
for (int i=0; i<headers.length; i++) {
System.out.println(headers[i]);
}
System.out.println("----------------------------------------");
//@@@ there is no entity, so we can't call close() there
//@@@ there is no need to call close() either, since the
//@@@ connection will have been released immediately
} finally {
//@@@ any kind of cleanup that should be performed?
}
} // main
private final static HttpClient createHttpClient() {
ClientConnectionManager ccm =
new ThreadSafeClientConnManager(getParams());
DefaultHttpClient dhc =
new DefaultHttpClient(getParams(), ccm);
BasicHttpProcessor bhp = dhc.getProcessor();
// Required protocol interceptors
bhp.addInterceptor(new RequestContent());
bhp.addInterceptor(new RequestTargetHost());
// Recommended protocol interceptors
bhp.addInterceptor(new RequestConnControl());
bhp.addInterceptor(new RequestUserAgent());
bhp.addInterceptor(new RequestExpectContinue());
return dhc;
}
/**
* Performs general setup.
* This should be called only once.
*/
private final static void setup() {
// Register the "http" and "https" protocol schemes, they are
// required by the default operator to look up socket factories.
SocketFactory sf = PlainSocketFactory.getSocketFactory();
SchemeRegistry.DEFAULT.register(new Scheme("http", sf, 80));
sf = SSLSocketFactory.getSocketFactory();
SchemeRegistry.DEFAULT.register(new Scheme("https", sf, 80));
// prepare parameters
HttpParams params = new DefaultHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, "UTF-8");
HttpProtocolParams.setUserAgent(params, "Jakarta-HttpClient/4.0");
HttpProtocolParams.setUseExpectContinue(params, true);
defaultParameters = params;
} // setup
private final static HttpParams getParams() {
return defaultParameters;
}
/**
* Creates a request to execute in this example.
* In a real application, request interceptors should be used
* to add the required headers.
*
* @param target the target server for the request
*
* @return a request without an entity
*/
private final static HttpRequest createRequest(HttpHost target) {
HttpRequest req = new BasicHttpRequest
("OPTIONS", "*", HttpVersion.HTTP_1_1);
req.addHeader("Host", target.getHostName());
return req;
}
} // class ClientExecuteProxy

View File

@ -81,7 +81,7 @@ public class ManagerConnectDirect {
private static HttpParams defaultParameters = null;
/**
* The scheme set.
* The scheme registry.
* Instantiated in {@link #setup setup}.
*/
private static SchemeRegistry supportedSchemes;

View File

@ -82,7 +82,7 @@ public class ManagerConnectProxy {
private static HttpParams defaultParameters = null;
/**
* The scheme set.
* The scheme registry.
* Instantiated in {@link #setup setup}.
*/
private static SchemeRegistry supportedSchemes;

View File

@ -79,7 +79,7 @@ public class OperatorConnectDirect {
private static HttpParams defaultParameters;
/**
* The scheme set.
* The scheme registry.
* Instantiated in {@link #setup setup}.
*/
private static SchemeRegistry supportedSchemes;

View File

@ -80,7 +80,7 @@ public class OperatorConnectProxy {
private static HttpParams defaultParameters = null;
/**
* The scheme set.
* The scheme registry.
* Instantiated in {@link #setup setup}.
*/
private static SchemeRegistry supportedSchemes;

View File

@ -326,7 +326,7 @@ public class SSLSocketFactory implements SecureSocketFactory {
throw new IllegalArgumentException("Socket is closed.");
}
return false;
return true;
} // isSecure