You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ro...@apache.org on 2007/02/10 09:28:36 UTC
svn commit: r505656 - in /jakarta/httpcomponents/httpclient/trunk/src:
examples/org/apache/http/examples/client/
examples/org/apache/http/examples/conn/ java/org/apache/http/conn/ssl/
Author: rolandw
Date: Sat Feb 10 00:28:36 2007
New Revision: 505656
URL: http://svn.apache.org/viewvc?view=rev&rev=505656
Log:
updated examples and a minor bug fix
Added:
jakarta/httpcomponents/httpclient/trunk/src/examples/org/apache/http/examples/client/ClientExecuteDirect.java
- copied, changed from r505650, jakarta/httpcomponents/httpclient/trunk/src/examples/org/apache/http/examples/client/ClientExecuteRequest.java
jakarta/httpcomponents/httpclient/trunk/src/examples/org/apache/http/examples/client/ClientExecuteProxy.java (with props)
Removed:
jakarta/httpcomponents/httpclient/trunk/src/examples/org/apache/http/examples/client/ClientExecuteRequest.java
Modified:
jakarta/httpcomponents/httpclient/trunk/src/examples/org/apache/http/examples/conn/ManagerConnectDirect.java
jakarta/httpcomponents/httpclient/trunk/src/examples/org/apache/http/examples/conn/ManagerConnectProxy.java
jakarta/httpcomponents/httpclient/trunk/src/examples/org/apache/http/examples/conn/OperatorConnectDirect.java
jakarta/httpcomponents/httpclient/trunk/src/examples/org/apache/http/examples/conn/OperatorConnectProxy.java
jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/ssl/SSLSocketFactory.java
Copied: jakarta/httpcomponents/httpclient/trunk/src/examples/org/apache/http/examples/client/ClientExecuteDirect.java (from r505650, jakarta/httpcomponents/httpclient/trunk/src/examples/org/apache/http/examples/client/ClientExecuteRequest.java)
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/examples/org/apache/http/examples/client/ClientExecuteDirect.java?view=diff&rev=505656&p1=jakarta/httpcomponents/httpclient/trunk/src/examples/org/apache/http/examples/client/ClientExecuteRequest.java&r1=505650&p2=jakarta/httpcomponents/httpclient/trunk/src/examples/org/apache/http/examples/client/ClientExecuteDirect.java&r2=505656
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/examples/org/apache/http/examples/client/ClientExecuteRequest.java (original)
+++ jakarta/httpcomponents/httpclient/trunk/src/examples/org/apache/http/examples/client/ClientExecuteDirect.java Sat Feb 10 00:28:36 2007
@@ -60,17 +60,17 @@
/**
- * 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 @@
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 @@
}
-} // class ManagerConnectDirect
+} // class ClientExecuteDirect
Added: jakarta/httpcomponents/httpclient/trunk/src/examples/org/apache/http/examples/client/ClientExecuteProxy.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/examples/org/apache/http/examples/client/ClientExecuteProxy.java?view=auto&rev=505656
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/examples/org/apache/http/examples/client/ClientExecuteProxy.java (added)
+++ jakarta/httpcomponents/httpclient/trunk/src/examples/org/apache/http/examples/client/ClientExecuteProxy.java Sat Feb 10 00:28:36 2007
@@ -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
+
Propchange: jakarta/httpcomponents/httpclient/trunk/src/examples/org/apache/http/examples/client/ClientExecuteProxy.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/httpcomponents/httpclient/trunk/src/examples/org/apache/http/examples/client/ClientExecuteProxy.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: jakarta/httpcomponents/httpclient/trunk/src/examples/org/apache/http/examples/client/ClientExecuteProxy.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: jakarta/httpcomponents/httpclient/trunk/src/examples/org/apache/http/examples/conn/ManagerConnectDirect.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/examples/org/apache/http/examples/conn/ManagerConnectDirect.java?view=diff&rev=505656&r1=505655&r2=505656
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/examples/org/apache/http/examples/conn/ManagerConnectDirect.java (original)
+++ jakarta/httpcomponents/httpclient/trunk/src/examples/org/apache/http/examples/conn/ManagerConnectDirect.java Sat Feb 10 00:28:36 2007
@@ -81,7 +81,7 @@
private static HttpParams defaultParameters = null;
/**
- * The scheme set.
+ * The scheme registry.
* Instantiated in {@link #setup setup}.
*/
private static SchemeRegistry supportedSchemes;
Modified: jakarta/httpcomponents/httpclient/trunk/src/examples/org/apache/http/examples/conn/ManagerConnectProxy.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/examples/org/apache/http/examples/conn/ManagerConnectProxy.java?view=diff&rev=505656&r1=505655&r2=505656
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/examples/org/apache/http/examples/conn/ManagerConnectProxy.java (original)
+++ jakarta/httpcomponents/httpclient/trunk/src/examples/org/apache/http/examples/conn/ManagerConnectProxy.java Sat Feb 10 00:28:36 2007
@@ -82,7 +82,7 @@
private static HttpParams defaultParameters = null;
/**
- * The scheme set.
+ * The scheme registry.
* Instantiated in {@link #setup setup}.
*/
private static SchemeRegistry supportedSchemes;
Modified: jakarta/httpcomponents/httpclient/trunk/src/examples/org/apache/http/examples/conn/OperatorConnectDirect.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/examples/org/apache/http/examples/conn/OperatorConnectDirect.java?view=diff&rev=505656&r1=505655&r2=505656
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/examples/org/apache/http/examples/conn/OperatorConnectDirect.java (original)
+++ jakarta/httpcomponents/httpclient/trunk/src/examples/org/apache/http/examples/conn/OperatorConnectDirect.java Sat Feb 10 00:28:36 2007
@@ -79,7 +79,7 @@
private static HttpParams defaultParameters;
/**
- * The scheme set.
+ * The scheme registry.
* Instantiated in {@link #setup setup}.
*/
private static SchemeRegistry supportedSchemes;
Modified: jakarta/httpcomponents/httpclient/trunk/src/examples/org/apache/http/examples/conn/OperatorConnectProxy.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/examples/org/apache/http/examples/conn/OperatorConnectProxy.java?view=diff&rev=505656&r1=505655&r2=505656
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/examples/org/apache/http/examples/conn/OperatorConnectProxy.java (original)
+++ jakarta/httpcomponents/httpclient/trunk/src/examples/org/apache/http/examples/conn/OperatorConnectProxy.java Sat Feb 10 00:28:36 2007
@@ -80,7 +80,7 @@
private static HttpParams defaultParameters = null;
/**
- * The scheme set.
+ * The scheme registry.
* Instantiated in {@link #setup setup}.
*/
private static SchemeRegistry supportedSchemes;
Modified: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/ssl/SSLSocketFactory.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/ssl/SSLSocketFactory.java?view=diff&rev=505656&r1=505655&r2=505656
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/ssl/SSLSocketFactory.java (original)
+++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/ssl/SSLSocketFactory.java Sat Feb 10 00:28:36 2007
@@ -326,7 +326,7 @@
throw new IllegalArgumentException("Socket is closed.");
}
- return false;
+ return true;
} // isSecure