From 09601a328fbba12b62f3efa08f4d8c7cb8886ef8 Mon Sep 17 00:00:00 2001
From: Oleg Kalnichevski
Date: Sat, 26 Sep 2009 11:56:06 +0000
Subject: [PATCH] Cleaned up examples
git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@819114 13f79535-47bb-0310-9956-ffa450edef68
---
.../examples/client/ClientExecuteDirect.java | 29 +----------
.../examples/client/ClientExecuteProxy.java | 36 +-------------
.../client/ClientKerberosAuthentication.java | 49 ++++++++++---------
.../client/ClientWithResponseHandler.java | 2 +-
4 files changed, 29 insertions(+), 87 deletions(-)
diff --git a/httpclient/src/examples/org/apache/http/examples/client/ClientExecuteDirect.java b/httpclient/src/examples/org/apache/http/examples/client/ClientExecuteDirect.java
index 398e8597e..ee6ecfdbe 100644
--- a/httpclient/src/examples/org/apache/http/examples/client/ClientExecuteDirect.java
+++ b/httpclient/src/examples/org/apache/http/examples/client/ClientExecuteDirect.java
@@ -31,18 +31,9 @@ import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
-import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
-import org.apache.http.conn.ClientConnectionManager;
-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.impl.client.DefaultHttpClient;
-import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
-import org.apache.http.params.BasicHttpParams;
-import org.apache.http.params.HttpParams;
-import org.apache.http.params.HttpProtocolParams;
import org.apache.http.util.EntityUtils;
/**
@@ -53,27 +44,9 @@ import org.apache.http.util.EntityUtils;
public class ClientExecuteDirect {
public static void main(String[] args) throws Exception {
+ DefaultHttpClient httpclient = new DefaultHttpClient();
HttpHost target = new HttpHost("www.apache.org", 80, "http");
-
- // general setup
- SchemeRegistry supportedSchemes = new SchemeRegistry();
-
- // Register the "http" protocol scheme, it is required
- // by the default operator to look up socket factories.
- 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);
-
- ClientConnectionManager connMgr = new ThreadSafeClientConnManager(params,
- supportedSchemes);
- DefaultHttpClient httpclient = new DefaultHttpClient(connMgr, params);
-
HttpGet req = new HttpGet("/");
System.out.println("executing request to " + target);
diff --git a/httpclient/src/examples/org/apache/http/examples/client/ClientExecuteProxy.java b/httpclient/src/examples/org/apache/http/examples/client/ClientExecuteProxy.java
index ed36e2be8..6336fd584 100644
--- a/httpclient/src/examples/org/apache/http/examples/client/ClientExecuteProxy.java
+++ b/httpclient/src/examples/org/apache/http/examples/client/ClientExecuteProxy.java
@@ -31,20 +31,10 @@ import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
-import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
-import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnRoutePNames;
-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.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
-import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
-import org.apache.http.params.BasicHttpParams;
-import org.apache.http.params.HttpParams;
-import org.apache.http.params.HttpProtocolParams;
import org.apache.http.util.EntityUtils;
/**
@@ -55,34 +45,12 @@ import org.apache.http.util.EntityUtils;
public class ClientExecuteProxy {
public static void main(String[] args)throws Exception {
-
- // make sure to use a proxy that supports CONNECT
- HttpHost target = new HttpHost("issues.apache.org", 443, "https");
HttpHost proxy = new HttpHost("127.0.0.1", 8080, "http");
- // 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.
- 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);
-
- ClientConnectionManager ccm = new ThreadSafeClientConnManager(params,
- supportedSchemes);
-
- DefaultHttpClient httpclient = new DefaultHttpClient(ccm, params);
-
+ DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
+ HttpHost target = new HttpHost("issues.apache.org", 443, "https");
HttpGet req = new HttpGet("/");
System.out.println("executing request to " + target + " via " + proxy);
diff --git a/httpclient/src/examples/org/apache/http/examples/client/ClientKerberosAuthentication.java b/httpclient/src/examples/org/apache/http/examples/client/ClientKerberosAuthentication.java
index 8cea6041c..2dd9079b0 100644
--- a/httpclient/src/examples/org/apache/http/examples/client/ClientKerberosAuthentication.java
+++ b/httpclient/src/examples/org/apache/http/examples/client/ClientKerberosAuthentication.java
@@ -41,8 +41,7 @@ import org.apache.http.util.EntityUtils;
/**
* Kerberos auth example.
*
- * Takes one arguement args[0] = 'http://examplehost/path/'
- * Information
+ * Information
* For the best compatibility use Java >= 1.6 as it supports SPNEGO authentication more
completely.
* NegotiateSchemeFactory
@@ -60,7 +59,7 @@ import org.apache.http.util.EntityUtils;
* Requires use of bouncy castle libs
*
*
- * Addtional Config Files
+ * Addtional Config Files
* Two files control how Java uses/configures Kerberos. Very basic examples are below. There
* is a large amount of information on the web.
* http://java.sun.com/j2se/1.5.0/docs/guide/security/jaas/spec/com/sun/security/auth/module/Krb5LoginModule.html
@@ -98,6 +97,29 @@ import org.apache.http.util.EntityUtils;
* com.sun.security.auth.module.Krb5LoginModule required client=TRUE useTicketCache=true debug=true;
*};
*
+ *
Windows specific configuration
+ *
+ * The registry key allowtgtsessionkey should be added, and set correctly, to allow
+ * session keys to be sent in the Kerberos Ticket-Granting Ticket.
+ *
+ *
+ * On the Windows Server 2003 and Windows 2000 SP4, here is the required registry setting:
+ *
+ *
+ * HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Lsa\Kerberos\Parameters
+ * Value Name: allowtgtsessionkey
+ * Value Type: REG_DWORD
+ * Value: 0x01
+ *
+ *
+ * Here is the location of the registry setting on Windows XP SP2:
+ *
+ *
+ * HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Lsa\Kerberos\
+ * Value Name: allowtgtsessionkey
+ * Value Type: REG_DWORD
+ * Value: 0x01
+ *
*
* @since 4.1
*/
@@ -110,27 +132,6 @@ public class ClientKerberosAuthentication {
System.setProperty("sun.security.krb5.debug", "true");
System.setProperty("javax.security.auth.useSubjectCredsOnly","false");
- /*
- * Below is helpful on windows.
- * Solution 2: You need to update the Windows registry to disable this new feature.
- * The registry key allowtgtsessionkey should be added--and set correctly--to allow
- * session keys to be sent in the Kerberos Ticket-Granting Ticket.
- *
- * On the Windows Server 2003 and Windows 2000 SP4, here is the required registry setting:
- *
- * HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Lsa\Kerberos\Parameters
- * Value Name: allowtgtsessionkey
- * Value Type: REG_DWORD
- * Value: 0x01
- *
- * Here is the location of the registry setting on Windows XP SP2:
- *
- * HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Lsa\Kerberos\
- * Value Name: allowtgtsessionkey
- * Value Type: REG_DWORD
- * Value: 0x01
- */
-
DefaultHttpClient httpclient = new DefaultHttpClient();
/*
diff --git a/httpclient/src/examples/org/apache/http/examples/client/ClientWithResponseHandler.java b/httpclient/src/examples/org/apache/http/examples/client/ClientWithResponseHandler.java
index 8ee38968e..10bff5c1d 100644
--- a/httpclient/src/examples/org/apache/http/examples/client/ClientWithResponseHandler.java
+++ b/httpclient/src/examples/org/apache/http/examples/client/ClientWithResponseHandler.java
@@ -50,8 +50,8 @@ public class ClientWithResponseHandler {
// Create a response handler
ResponseHandler responseHandler = new BasicResponseHandler();
String responseBody = httpclient.execute(httpget, responseHandler);
+ System.out.println("----------------------------------------");
System.out.println(responseBody);
-
System.out.println("----------------------------------------");
// When HttpClient instance is no longer needed,