Subversion Repositories javautils

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * ====================================================================
  3.  * Licensed to the Apache Software Foundation (ASF) under one
  4.  * or more contributor license agreements.  See the NOTICE file
  5.  * distributed with this work for additional information
  6.  * regarding copyright ownership.  The ASF licenses this file
  7.  * to you under the Apache License, Version 2.0 (the
  8.  * "License"); you may not use this file except in compliance
  9.  * with the License.  You may obtain a copy of the License at
  10.  *
  11.  *   http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing,
  14.  * software distributed under the License is distributed on an
  15.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16.  * KIND, either express or implied.  See the License for the
  17.  * specific language governing permissions and limitations
  18.  * under the License.
  19.  * ====================================================================
  20.  *
  21.  * This software consists of voluntary contributions made by many
  22.  * individuals on behalf of the Apache Software Foundation.  For more
  23.  * information on the Apache Software Foundation, please see
  24.  * <http://www.apache.org/>.
  25.  *
  26.  */
  27.  
  28. package org.apache.http.examples.conn;
  29.  
  30. import org.apache.http.Header;
  31. import org.apache.http.HttpHost;
  32. import org.apache.http.HttpRequest;
  33. import org.apache.http.HttpResponse;
  34. import org.apache.http.HttpVersion;
  35. import org.apache.http.conn.ClientConnectionOperator;
  36. import org.apache.http.conn.OperatedClientConnection;
  37. import org.apache.http.conn.scheme.PlainSocketFactory;
  38. import org.apache.http.conn.scheme.Scheme;
  39. import org.apache.http.conn.scheme.SchemeRegistry;
  40. import org.apache.http.conn.scheme.SocketFactory;
  41. import org.apache.http.impl.conn.DefaultClientConnectionOperator;
  42. import org.apache.http.message.BasicHttpRequest;
  43. import org.apache.http.params.BasicHttpParams;
  44. import org.apache.http.params.HttpParams;
  45. import org.apache.http.params.HttpProtocolParams;
  46. import org.apache.http.protocol.HttpContext;
  47. import org.apache.http.protocol.BasicHttpContext;
  48.  
  49.  
  50. /**
  51.  * How to open a direct connection using
  52.  * {@link ClientConnectionOperator ClientConnectionOperator}.
  53.  * This exemplifies the <i>opening</i> of the connection only.
  54.  * The subsequent message exchange in this example should not
  55.  * be used as a template.
  56.  *
  57.  * @since 4.0
  58.  */
  59. public class OperatorConnectDirect {
  60.  
  61.     public static void main(String[] args) throws Exception {
  62.         HttpHost target = new HttpHost("jakarta.apache.org", 80, "http");
  63.  
  64.         // some general setup
  65.         // Register the "http" protocol scheme, it is required
  66.         // by the default operator to look up socket factories.
  67.         SchemeRegistry supportedSchemes = new SchemeRegistry();
  68.         SocketFactory sf = PlainSocketFactory.getSocketFactory();
  69.         supportedSchemes.register(new Scheme("http", sf, 80));
  70.  
  71.         // Prepare parameters.
  72.         // Since this example doesn't use the full core framework,
  73.         // only few parameters are actually required.
  74.         HttpParams params = new BasicHttpParams();
  75.         HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
  76.         HttpProtocolParams.setUseExpectContinue(params, false);
  77.  
  78.         // one operator can be used for many connections
  79.         ClientConnectionOperator scop = new DefaultClientConnectionOperator(supportedSchemes);
  80.  
  81.         HttpRequest req = new BasicHttpRequest("OPTIONS", "*", HttpVersion.HTTP_1_1);
  82.         req.addHeader("Host", target.getHostName());
  83.        
  84.         HttpContext ctx = new BasicHttpContext();
  85.  
  86.         OperatedClientConnection conn = scop.createConnection();
  87.         try {
  88.             System.out.println("opening connection to " + target);
  89.             scop.openConnection(conn, target, null, ctx, params);
  90.             System.out.println("sending request");
  91.             conn.sendRequestHeader(req);
  92.             // there is no request entity
  93.             conn.flush();
  94.  
  95.             System.out.println("receiving response header");
  96.             HttpResponse rsp = conn.receiveResponseHeader();
  97.  
  98.             System.out.println("----------------------------------------");
  99.             System.out.println(rsp.getStatusLine());
  100.             Header[] headers = rsp.getAllHeaders();
  101.             for (int i = 0; i < headers.length; i++) {
  102.                 System.out.println(headers[i]);
  103.             }
  104.             System.out.println("----------------------------------------");
  105.         } finally {
  106.             System.out.println("closing connection");
  107.             conn.close();
  108.         }
  109.     }
  110.  
  111. }
  112.  
  113.