Cleaned up more sample code
git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@748834 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
65b8b329ec
commit
d3389a9b7b
|
@ -31,7 +31,6 @@
|
|||
|
||||
package org.apache.http.examples.conn;
|
||||
|
||||
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.HttpRequest;
|
||||
|
@ -52,7 +51,6 @@ import org.apache.http.protocol.HttpContext;
|
|||
import org.apache.http.protocol.BasicHttpContext;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* How to open a direct connection using
|
||||
* {@link ClientConnectionOperator ClientConnectionOperator}.
|
||||
|
@ -60,50 +58,39 @@ import org.apache.http.protocol.BasicHttpContext;
|
|||
* The subsequent message exchange in this example should not
|
||||
* be used as a template.
|
||||
*
|
||||
*
|
||||
*
|
||||
* <!-- empty lines above to avoid 'svn diff' context problems -->
|
||||
* @version $Revision$ $Date$
|
||||
*
|
||||
* @since 4.0
|
||||
*/
|
||||
public class OperatorConnectDirect {
|
||||
|
||||
/**
|
||||
* The default parameters.
|
||||
* Instantiated in {@link #setup setup}.
|
||||
*/
|
||||
private static HttpParams defaultParameters;
|
||||
public static void main(String[] args) throws Exception {
|
||||
HttpHost target = new HttpHost("jakarta.apache.org", 80, "http");
|
||||
|
||||
/**
|
||||
* The scheme registry.
|
||||
* Instantiated in {@link #setup setup}.
|
||||
*/
|
||||
private static SchemeRegistry supportedSchemes;
|
||||
// some general setup
|
||||
// Register the "http" protocol scheme, it is required
|
||||
// by the default operator to look up socket factories.
|
||||
SchemeRegistry supportedSchemes = new SchemeRegistry();
|
||||
SocketFactory sf = PlainSocketFactory.getSocketFactory();
|
||||
supportedSchemes.register(new Scheme("http", sf, 80));
|
||||
|
||||
|
||||
/**
|
||||
* Main entry point to this example.
|
||||
*
|
||||
* @param args ignored
|
||||
*/
|
||||
public final static void main(String[] args)
|
||||
throws Exception {
|
||||
|
||||
final HttpHost target = new HttpHost("jakarta.apache.org", 80, "http");
|
||||
|
||||
setup(); // some general setup
|
||||
// Prepare parameters.
|
||||
// Since this example doesn't use the full core framework,
|
||||
// only few parameters are actually required.
|
||||
HttpParams params = new BasicHttpParams();
|
||||
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
|
||||
HttpProtocolParams.setUseExpectContinue(params, false);
|
||||
|
||||
// one operator can be used for many connections
|
||||
ClientConnectionOperator scop = createOperator();
|
||||
ClientConnectionOperator scop = new DefaultClientConnectionOperator(supportedSchemes);
|
||||
|
||||
HttpRequest req = new BasicHttpRequest("OPTIONS", "*", HttpVersion.HTTP_1_1);
|
||||
req.addHeader("Host", target.getHostName());
|
||||
|
||||
HttpContext ctx = new BasicHttpContext();
|
||||
|
||||
OperatedClientConnection conn = scop.createConnection();
|
||||
|
||||
HttpRequest req = createRequest(target);
|
||||
HttpContext ctx = createContext();
|
||||
|
||||
try {
|
||||
System.out.println("opening connection to " + target);
|
||||
scop.openConnection(conn, target, null, ctx, getParams());
|
||||
|
||||
scop.openConnection(conn, target, null, ctx, params);
|
||||
System.out.println("sending request");
|
||||
conn.sendRequestHeader(req);
|
||||
// there is no request entity
|
||||
|
@ -119,76 +106,11 @@ public class OperatorConnectDirect {
|
|||
System.out.println(headers[i]);
|
||||
}
|
||||
System.out.println("----------------------------------------");
|
||||
|
||||
} finally {
|
||||
System.out.println("closing connection");
|
||||
conn.close();
|
||||
|
||||
} // main
|
||||
|
||||
|
||||
private final static ClientConnectionOperator createOperator() {
|
||||
return new DefaultClientConnectionOperator(supportedSchemes);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Performs general setup.
|
||||
* This should be called only once.
|
||||
*/
|
||||
private final static void setup() {
|
||||
|
||||
// Register the "http" protocol scheme, it is required
|
||||
// by the default operator to look up socket factories.
|
||||
supportedSchemes = new SchemeRegistry();
|
||||
SocketFactory sf = PlainSocketFactory.getSocketFactory();
|
||||
supportedSchemes.register(new Scheme("http", sf, 80));
|
||||
|
||||
// Prepare parameters.
|
||||
// Since this example doesn't use the full core framework,
|
||||
// only few parameters are actually required.
|
||||
HttpParams params = new BasicHttpParams();
|
||||
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
|
||||
HttpProtocolParams.setUseExpectContinue(params, false);
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a context for executing a request.
|
||||
* Since this example doesn't really use the execution framework,
|
||||
* the context can be left empty.
|
||||
*
|
||||
* @return a new, empty context
|
||||
*/
|
||||
private final static HttpContext createContext() {
|
||||
return new BasicHttpContext(null);
|
||||
}
|
||||
|
||||
} // class OperatorConnectDirect
|
||||
|
||||
|
|
|
@ -31,7 +31,6 @@
|
|||
|
||||
package org.apache.http.examples.conn;
|
||||
|
||||
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.HttpRequest;
|
||||
|
@ -42,7 +41,6 @@ import org.apache.http.conn.OperatedClientConnection;
|
|||
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.conn.DefaultClientConnectionOperator;
|
||||
import org.apache.http.message.BasicHttpRequest;
|
||||
|
@ -52,8 +50,6 @@ import org.apache.http.params.HttpProtocolParams;
|
|||
import org.apache.http.protocol.HttpContext;
|
||||
import org.apache.http.protocol.BasicHttpContext;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* How to open a secure connection through a proxy using
|
||||
* {@link ClientConnectionOperator ClientConnectionOperator}.
|
||||
|
@ -61,55 +57,56 @@ import org.apache.http.protocol.BasicHttpContext;
|
|||
* The message exchange, both subsequently and for tunnelling,
|
||||
* should not be used as a template.
|
||||
*
|
||||
*
|
||||
*
|
||||
* <!-- empty lines above to avoid 'svn diff' context problems -->
|
||||
* @version $Revision$ $Date$
|
||||
*
|
||||
* @since 4.0
|
||||
*/
|
||||
public class OperatorConnectProxy {
|
||||
|
||||
/**
|
||||
* 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");
|
||||
HttpHost target = new HttpHost("issues.apache.org", 443, "https");
|
||||
HttpHost proxy = new HttpHost("127.0.0.1", 8666, "http");
|
||||
|
||||
setup(); // some general setup
|
||||
// some general setup
|
||||
// Register the "http" and "https" protocol schemes, they are
|
||||
// required by the default operator to look up socket factories.
|
||||
SchemeRegistry supportedSchemes = new SchemeRegistry();
|
||||
supportedSchemes.register(new Scheme("http",
|
||||
PlainSocketFactory.getSocketFactory(), 80));
|
||||
supportedSchemes.register(new Scheme("https",
|
||||
SSLSocketFactory.getSocketFactory(), 443));
|
||||
|
||||
// Prepare parameters.
|
||||
// Since this example doesn't use the full core framework,
|
||||
// only few parameters are actually required.
|
||||
HttpParams params = new BasicHttpParams();
|
||||
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
|
||||
HttpProtocolParams.setUseExpectContinue(params, false);
|
||||
|
||||
// one operator can be used for many connections
|
||||
ClientConnectionOperator scop = createOperator();
|
||||
ClientConnectionOperator scop = new DefaultClientConnectionOperator(supportedSchemes);
|
||||
|
||||
HttpRequest req = new BasicHttpRequest("OPTIONS", "*", HttpVersion.HTTP_1_1);
|
||||
// In a real application, request interceptors should be used
|
||||
// to add the required headers.
|
||||
req.addHeader("Host", target.getHostName());
|
||||
|
||||
HttpContext ctx = new BasicHttpContext();
|
||||
|
||||
OperatedClientConnection conn = scop.createConnection();
|
||||
|
||||
HttpRequest req = createRequest(target);
|
||||
HttpContext ctx = createContext();
|
||||
|
||||
try {
|
||||
System.out.println("opening connection to " + proxy);
|
||||
scop.openConnection(conn, proxy, null, ctx, getParams());
|
||||
scop.openConnection(conn, proxy, null, ctx, params);
|
||||
|
||||
// Creates a request to tunnel a connection.
|
||||
// For details see RFC 2817, section 5.2
|
||||
String authority = target.getHostName() + ":" + target.getPort();
|
||||
HttpRequest connect = new BasicHttpRequest("CONNECT", authority,
|
||||
HttpVersion.HTTP_1_1);
|
||||
// In a real application, request interceptors should be used
|
||||
// to add the required headers.
|
||||
connect.addHeader("Host", authority);
|
||||
|
||||
HttpRequest connect = createConnect(target);
|
||||
System.out.println("opening tunnel to " + target);
|
||||
conn.sendRequestHeader(connect);
|
||||
// there is no request entity
|
||||
|
@ -133,10 +130,10 @@ public class OperatorConnectProxy {
|
|||
// connection with the new target is optional - but good style.
|
||||
// The scheme part of the target is already "https", though the
|
||||
// connection is not yet switched to the TLS/SSL protocol.
|
||||
conn.update(null, target, false, getParams());
|
||||
conn.update(null, target, false, params);
|
||||
|
||||
System.out.println("layering secure connection");
|
||||
scop.updateSecureConnection(conn, target, ctx, getParams());
|
||||
scop.updateSecureConnection(conn, target, ctx, params);
|
||||
|
||||
// finally we have the secure connection and can send the request
|
||||
|
||||
|
@ -152,104 +149,13 @@ public class OperatorConnectProxy {
|
|||
printResponseHeader(rsp);
|
||||
System.out.println("----------------------------------------");
|
||||
|
||||
} finally {
|
||||
System.out.println("closing connection");
|
||||
conn.close();
|
||||
|
||||
} // main
|
||||
|
||||
|
||||
private final static ClientConnectionOperator createOperator() {
|
||||
return new DefaultClientConnectionOperator(supportedSchemes);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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.
|
||||
supportedSchemes = new SchemeRegistry();
|
||||
SocketFactory sf = PlainSocketFactory.getSocketFactory();
|
||||
supportedSchemes.register(new Scheme("http", sf, 80));
|
||||
sf = SSLSocketFactory.getSocketFactory();
|
||||
supportedSchemes.register(new Scheme("https", sf, 80));
|
||||
|
||||
// Prepare parameters.
|
||||
// Since this example doesn't use the full core framework,
|
||||
// only few parameters are actually required.
|
||||
HttpParams params = new BasicHttpParams();
|
||||
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
|
||||
HttpProtocolParams.setUseExpectContinue(params, false);
|
||||
defaultParameters = params;
|
||||
|
||||
} // setup
|
||||
|
||||
|
||||
private final static HttpParams getParams() {
|
||||
return defaultParameters;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a request to tunnel a connection.
|
||||
* In a real application, request interceptors should be used
|
||||
* to add the required headers.
|
||||
*
|
||||
* @param target the target server for the tunnel
|
||||
*
|
||||
* @return a CONNECT request without an entity
|
||||
*/
|
||||
private final static HttpRequest createConnect(HttpHost target) {
|
||||
|
||||
// see RFC 2817, section 5.2
|
||||
final String authority = target.getHostName()+":"+target.getPort();
|
||||
|
||||
HttpRequest req = new BasicHttpRequest
|
||||
("CONNECT", authority, HttpVersion.HTTP_1_1);
|
||||
|
||||
req.addHeader("Host", authority);
|
||||
|
||||
return req;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a context for executing a request.
|
||||
* Since this example doesn't really use the execution framework,
|
||||
* the context can be left empty.
|
||||
*
|
||||
* @return a new, empty context
|
||||
*/
|
||||
private final static HttpContext createContext() {
|
||||
return new BasicHttpContext(null);
|
||||
}
|
||||
|
||||
|
||||
private final static void printResponseHeader(HttpResponse rsp) {
|
||||
|
||||
System.out.println(rsp.getStatusLine());
|
||||
Header[] headers = rsp.getAllHeaders();
|
||||
for (int i=0; i<headers.length; i++) {
|
||||
|
@ -257,5 +163,5 @@ public class OperatorConnectProxy {
|
|||
}
|
||||
}
|
||||
|
||||
} // class OperatorConnectProxy
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue