You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2009/02/28 13:05:45 UTC

svn commit: r748828 - in /httpcomponents/httpclient/trunk/module-client/src/examples/org/apache/http/examples/client: ClientExecuteDirect.java ClientExecuteProxy.java

Author: olegk
Date: Sat Feb 28 12:05:44 2009
New Revision: 748828

URL: http://svn.apache.org/viewvc?rev=748828&view=rev
Log:
Cleaned up execute direct and execute via proxy examples

Modified:
    httpcomponents/httpclient/trunk/module-client/src/examples/org/apache/http/examples/client/ClientExecuteDirect.java
    httpcomponents/httpclient/trunk/module-client/src/examples/org/apache/http/examples/client/ClientExecuteProxy.java

Modified: httpcomponents/httpclient/trunk/module-client/src/examples/org/apache/http/examples/client/ClientExecuteDirect.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-client/src/examples/org/apache/http/examples/client/ClientExecuteDirect.java?rev=748828&r1=748827&r2=748828&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/module-client/src/examples/org/apache/http/examples/client/ClientExecuteDirect.java (original)
+++ httpcomponents/httpclient/trunk/module-client/src/examples/org/apache/http/examples/client/ClientExecuteDirect.java Sat Feb 28 12:05:44 2009
@@ -31,7 +31,6 @@
 
 package org.apache.http.examples.client;
 
-
 import org.apache.http.Header;
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpHost;
@@ -43,7 +42,6 @@
 import org.apache.http.conn.scheme.PlainSocketFactory;
 import org.apache.http.conn.scheme.Scheme;
 import org.apache.http.conn.scheme.SchemeRegistry;
-import org.apache.http.conn.scheme.SocketFactory;
 import org.apache.http.impl.client.DefaultHttpClient;
 import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
 import org.apache.http.message.BasicHttpRequest;
@@ -52,134 +50,53 @@
 import org.apache.http.params.HttpProtocolParams;
 import org.apache.http.util.EntityUtils;
 
-
-
 /**
- * How to send a request directly using {@link HttpClient HttpClient}.
- *
- *
- *
- * <!-- empty lines above to avoid 'svn diff' context problems -->
- * @version $Revision$
- *
+ * How to send a request directly using {@link HttpClient}.
+ * 
  * @since 4.0
  */
 public class ClientExecuteDirect {
 
-    /**
-     * The default parameters.
-     * Instantiated in {@link #setup setup}.
-     */
-    private static HttpParams defaultParameters = null;
-
-    /**
-     * The scheme registry.
-     * Instantiated in {@link #setup setup}.
-     */
-    private static SchemeRegistry supportedSchemes;
-
-
-    /**
-     * Main entry point to this example.
-     *
-     * @param args      ignored
-     */
-    public final static void main(String[] args)
-        throws Exception {
-
-        final HttpHost target = new HttpHost("issues.apache.org", 80, "http");
-
-        setup(); // some general setup
-
-        HttpClient client = createHttpClient();
-
-        HttpRequest req = createRequest();
-
-        System.out.println("executing request to " + target);
-        HttpEntity entity = null;
-        try {
-            HttpResponse rsp = client.execute(target, req);
-            entity = rsp.getEntity();
-
-            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("----------------------------------------");
-
-            if (entity != null) {
-                System.out.println(EntityUtils.toString(rsp.getEntity()));
-            }
-
-        } finally {
-            // If we could be sure that the stream of the entity has been
-            // closed, we wouldn't need this code to release the connection.
-            // However, EntityUtils.toString(...) can throw an exception.
-
-            // if there is no entity, the connection is already released
-            if (entity != null)
-                entity.consumeContent(); // release connection gracefully
-        }
-    } // main
-
-
-    private final static HttpClient createHttpClient() {
+    public static void main(String[] args) throws Exception {
 
-        ClientConnectionManager ccm =
-            new ThreadSafeClientConnManager(getParams(), supportedSchemes);
-        //  new SingleClientConnManager(getParams(), supportedSchemes);
+        HttpHost target = new HttpHost("www.apache.org", 80, "http");
 
-        DefaultHttpClient dhc =
-            new DefaultHttpClient(ccm, getParams());
-
-        return dhc;
-    }
-
-
-    /**
-     * Performs general setup.
-     * This should be called only once.
-     */
-    private final static void setup() {
-
-        supportedSchemes = new SchemeRegistry();
+        // general setup
+        SchemeRegistry supportedSchemes = new SchemeRegistry();
 
         // Register the "http" protocol scheme, it is required
         // by the default operator to look up socket factories.
-        SocketFactory sf = PlainSocketFactory.getSocketFactory();
-        supportedSchemes.register(new Scheme("http", sf, 80));
+        supportedSchemes.register(new Scheme("http", 
+                PlainSocketFactory.getSocketFactory(), 80));
 
         // prepare parameters
         HttpParams params = new BasicHttpParams();
         HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
         HttpProtocolParams.setContentCharset(params, "UTF-8");
         HttpProtocolParams.setUseExpectContinue(params, true);
-        defaultParameters = params;
 
-    } // setup
+        ClientConnectionManager connMgr = new ThreadSafeClientConnManager(params, 
+                supportedSchemes);
+        DefaultHttpClient httpclient = new DefaultHttpClient(connMgr, params);
 
+        HttpRequest req = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1);
 
-    private final static HttpParams getParams() {
-        return defaultParameters;
-    }
+        System.out.println("executing request to " + target);
 
+        HttpResponse rsp = httpclient.execute(target, req);
+        HttpEntity entity = rsp.getEntity();
 
-    /**
-     * Creates a request to execute in this example.
-     *
-     * @return  a request without an entity
-     */
-    private final static HttpRequest createRequest() {
-
-        HttpRequest req = new BasicHttpRequest
-            ("GET", "/", HttpVersion.HTTP_1_1);
-          //("OPTIONS", "*", HttpVersion.HTTP_1_1);
+        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("----------------------------------------");
 
-        return req;
+        if (entity != null) {
+            System.out.println(EntityUtils.toString(entity));
+        }
     }
 
-
-} // class ClientExecuteDirect
-
+}

Modified: httpcomponents/httpclient/trunk/module-client/src/examples/org/apache/http/examples/client/ClientExecuteProxy.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-client/src/examples/org/apache/http/examples/client/ClientExecuteProxy.java?rev=748828&r1=748827&r2=748828&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/module-client/src/examples/org/apache/http/examples/client/ClientExecuteProxy.java (original)
+++ httpcomponents/httpclient/trunk/module-client/src/examples/org/apache/http/examples/client/ClientExecuteProxy.java Sat Feb 28 12:05:44 2009
@@ -31,7 +31,6 @@
 
 package org.apache.http.examples.client;
 
-
 import org.apache.http.Header;
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpHost;
@@ -44,7 +43,6 @@
 import org.apache.http.conn.scheme.PlainSocketFactory;
 import org.apache.http.conn.scheme.Scheme;
 import org.apache.http.conn.scheme.SchemeRegistry;
-import org.apache.http.conn.scheme.SocketFactory;
 import org.apache.http.conn.ssl.SSLSocketFactory;
 import org.apache.http.impl.client.DefaultHttpClient;
 import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
@@ -54,142 +52,59 @@
 import org.apache.http.params.HttpProtocolParams;
 import org.apache.http.util.EntityUtils;
 
-
-
 /**
- * How to send a request via proxy using {@link HttpClient HttpClient}.
- *
- *
- *
- * <!-- empty lines above to avoid 'svn diff' context problems -->
- * @version $Revision$
+ * How to send a request via proxy using {@link HttpClient}.
  *
  * @since 4.0
  */
 public class ClientExecuteProxy {
 
-    /**
-     * The default parameters.
-     * Instantiated in {@link #setup setup}.
-     */
-    private static HttpParams defaultParameters = null;
-
-    /**
-     * The scheme registry.
-     * Instantiated in {@link #setup setup}.
-     */
-    private static SchemeRegistry supportedSchemes;
-
-
-    /**
-     * Main entry point to this example.
-     *
-     * @param args      ignored
-     */
-    public final static void main(String[] args)
-        throws Exception {
+    public 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();
-
-        client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
-
-        HttpRequest req = createRequest();
-
-        System.out.println("executing request to " + target + " via " + proxy);
-        HttpEntity entity = null;
-        try {
-            HttpResponse rsp = client.execute(target, req);
-            entity = rsp.getEntity();
-
-            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("----------------------------------------");
-
-            if (rsp.getEntity() != null) {
-                System.out.println(EntityUtils.toString(rsp.getEntity()));
-            }
-
-        } finally {
-            // If we could be sure that the stream of the entity has been
-            // closed, we wouldn't need this code to release the connection.
-            // However, EntityUtils.toString(...) can throw an exception.
-
-            // if there is no entity, the connection is already released
-            if (entity != null)
-                entity.consumeContent(); // release connection gracefully
-        }
-    } // main
-
+        HttpHost target = new HttpHost("issues.apache.org", 443, "https");
+        HttpHost proxy = new HttpHost("127.0.0.1", 8080, "http");
 
-    private final static HttpClient createHttpClient() {
-
-        ClientConnectionManager ccm =
-            new ThreadSafeClientConnManager(getParams(), supportedSchemes);
-        //  new SingleClientConnManager(getParams(), supportedSchemes);
-
-        DefaultHttpClient dhc =
-            new DefaultHttpClient(ccm, getParams());
-
-        return dhc;
-    }
-
-
-    /**
-     * Performs general setup.
-     * This should be called only once.
-     */
-    private final static void setup() {
-
-        supportedSchemes = new SchemeRegistry();
+        // general setup
+        SchemeRegistry supportedSchemes = new SchemeRegistry();
 
         // Register the "http" and "https" protocol schemes, they are
         // required by the default operator to look up socket factories.
-        SocketFactory sf = PlainSocketFactory.getSocketFactory();
-        supportedSchemes.register(new Scheme("http", sf, 80));
-        sf = SSLSocketFactory.getSocketFactory();
-        supportedSchemes.register(new Scheme("https", sf, 80));
+        supportedSchemes.register(new Scheme("http", 
+                PlainSocketFactory.getSocketFactory(), 80));
+        supportedSchemes.register(new Scheme("https", 
+                SSLSocketFactory.getSocketFactory(), 443));
 
         // prepare parameters
         HttpParams params = new BasicHttpParams();
         HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
         HttpProtocolParams.setContentCharset(params, "UTF-8");
         HttpProtocolParams.setUseExpectContinue(params, true);
-        defaultParameters = params;
 
-    } // setup
+        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, 
+                supportedSchemes);
 
+        DefaultHttpClient httpclient = new DefaultHttpClient(ccm, params);
 
-    private final static HttpParams getParams() {
-        return defaultParameters;
-    }
-
+        httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
 
-    /**
-     * Creates a request to execute in this example.
-     *
-     * @return  a request without an entity
-     */
-    private final static HttpRequest createRequest() {
-
-        HttpRequest req = new BasicHttpRequest
-            ("GET", "/", HttpVersion.HTTP_1_1);
-          //("OPTIONS", "*", HttpVersion.HTTP_1_1);
+        HttpRequest req = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1);
 
-        return req;
-    }
+        System.out.println("executing request to " + target + " via " + proxy);
+        HttpResponse rsp = httpclient.execute(target, req);
+        HttpEntity entity = rsp.getEntity();
 
+        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("----------------------------------------");
 
-} // class ClientExecuteProxy
+        if (entity != null) {
+            System.out.println(EntityUtils.toString(entity));
+        }
+    }
 
+}
\ No newline at end of file