List of usage examples for org.apache.http.conn OperatedClientConnection update
void update(Socket sock, HttpHost target, boolean secure, HttpParams params) throws IOException;
From source file:com.dlmu.heipacker.crawler.examples.conn.OperatorConnectProxy.java
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", 8666, "http"); // 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", 80, PlainSocketFactory.getSocketFactory())); supportedSchemes.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory())); // Prepare parameters. // Since this example doesn't use the full core framework, // only few parameters are actually required. HttpParams params = new SyncBasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setUseExpectContinue(params, false); // one operator can be used for many connections 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(); try {//from www . j ava 2s.c o m System.out.println("opening connection to " + proxy); 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); System.out.println("opening tunnel to " + target); conn.sendRequestHeader(connect); // there is no request entity conn.flush(); System.out.println("receiving confirmation for tunnel"); HttpResponse connected = conn.receiveResponseHeader(); System.out.println("----------------------------------------"); printResponseHeader(connected); System.out.println("----------------------------------------"); int status = connected.getStatusLine().getStatusCode(); if ((status < 200) || (status > 299)) { System.out.println("unexpected status code " + status); System.exit(1); } System.out.println("receiving response body (ignored)"); conn.receiveResponseEntity(connected); // Now we have a tunnel to the target. As we will be creating a // layered TLS/SSL socket immediately afterwards, updating the // 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, params); System.out.println("layering secure connection"); scop.updateSecureConnection(conn, target, ctx, params); // finally we have the secure connection and can send the request System.out.println("sending request"); conn.sendRequestHeader(req); // there is no request entity conn.flush(); System.out.println("receiving response header"); HttpResponse rsp = conn.receiveResponseHeader(); System.out.println("----------------------------------------"); printResponseHeader(rsp); System.out.println("----------------------------------------"); } finally { System.out.println("closing connection"); conn.close(); } }
From source file:httpclient.conn.OperatorConnectProxy.java
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", 8666, "http"); // 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 = 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(); try {// w w w . jav a2s .c o m System.out.println("opening connection to " + proxy); 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); System.out.println("opening tunnel to " + target); conn.sendRequestHeader(connect); // there is no request entity conn.flush(); System.out.println("receiving confirmation for tunnel"); HttpResponse connected = conn.receiveResponseHeader(); System.out.println("----------------------------------------"); printResponseHeader(connected); System.out.println("----------------------------------------"); int status = connected.getStatusLine().getStatusCode(); if ((status < 200) || (status > 299)) { System.out.println("unexpected status code " + status); System.exit(1); } System.out.println("receiving response body (ignored)"); conn.receiveResponseEntity(connected); // Now we have a tunnel to the target. As we will be creating a // layered TLS/SSL socket immediately afterwards, updating the // 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, params); System.out.println("layering secure connection"); scop.updateSecureConnection(conn, target, ctx, params); // finally we have the secure connection and can send the request System.out.println("sending request"); conn.sendRequestHeader(req); // there is no request entity conn.flush(); System.out.println("receiving response header"); HttpResponse rsp = conn.receiveResponseHeader(); System.out.println("----------------------------------------"); printResponseHeader(rsp); System.out.println("----------------------------------------"); } finally { System.out.println("closing connection"); conn.close(); } }
From source file:com.qiniu.android.http.ClientConnectionOperator.java
public void updateSecureConnection(OperatedClientConnection conn, HttpHost target, HttpContext context, HttpParams params) throws IOException { if (conn == null) { throw new IllegalArgumentException("Connection must not be null."); }/*from w ww . j ava 2 s. c o m*/ if (target == null) { throw new IllegalArgumentException("Target host must not be null."); } if (params == null) { throw new IllegalArgumentException("Parameters must not be null."); } if (!conn.isOpen()) { throw new IllegalArgumentException("Connection must be open."); } final Scheme schm = schemeRegistry.getScheme(target.getSchemeName()); if (!(schm.getSocketFactory() instanceof LayeredSocketFactory)) { throw new IllegalArgumentException( "Target scheme (" + schm.getName() + ") must have layered socket factory."); } final LayeredSocketFactory lsf = (LayeredSocketFactory) schm.getSocketFactory(); final Socket sock; try { sock = lsf.createSocket(conn.getSocket(), target.getHostName(), target.getPort(), true); } catch (ConnectException ex) { throw new HttpHostConnectException(target, ex); } prepareSocket(sock, context, params); conn.update(sock, target, lsf.isSecure(sock), params); }
From source file:org.apache.http.impl.conn.DefaultClientConnectionOperator.java
public void updateSecureConnection(final OperatedClientConnection conn, final HttpHost target, final HttpContext context, final HttpParams params) throws IOException { Args.notNull(conn, "Connection"); Args.notNull(target, "Target host"); Args.notNull(params, "Parameters"); Asserts.check(conn.isOpen(), "Connection must be open"); final SchemeRegistry registry = getSchemeRegistry(context); final Scheme schm = registry.getScheme(target.getSchemeName()); Asserts.check(schm.getSchemeSocketFactory() instanceof SchemeLayeredSocketFactory, "Socket factory must implement SchemeLayeredSocketFactory"); final SchemeLayeredSocketFactory lsf = (SchemeLayeredSocketFactory) schm.getSchemeSocketFactory(); final Socket sock = lsf.createLayeredSocket(conn.getSocket(), target.getHostName(), schm.resolvePort(target.getPort()), params); prepareSocket(sock, context, params); conn.update(sock, target, lsf.isSecure(sock), params); }