View Javadoc
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.hc.core5.http.examples;
29  
30  import java.util.concurrent.TimeUnit;
31  
32  import org.apache.hc.core5.http.ClassicHttpRequest;
33  import org.apache.hc.core5.http.ClassicHttpResponse;
34  import org.apache.hc.core5.http.HttpConnection;
35  import org.apache.hc.core5.http.HttpHost;
36  import org.apache.hc.core5.http.HttpRequest;
37  import org.apache.hc.core5.http.HttpResponse;
38  import org.apache.hc.core5.http.Method;
39  import org.apache.hc.core5.http.impl.Http1StreamListener;
40  import org.apache.hc.core5.http.impl.bootstrap.HttpRequester;
41  import org.apache.hc.core5.http.impl.bootstrap.RequesterBootstrap;
42  import org.apache.hc.core5.http.io.SocketConfig;
43  import org.apache.hc.core5.http.io.entity.EntityUtils;
44  import org.apache.hc.core5.http.message.BasicClassicHttpRequest;
45  import org.apache.hc.core5.http.message.RequestLine;
46  import org.apache.hc.core5.http.message.StatusLine;
47  import org.apache.hc.core5.http.protocol.HttpCoreContext;
48  import org.apache.hc.core5.util.Timeout;
49  
50  /**
51   * Example of GET requests execution using classic I/O.
52   */
53  public class ClassicGetExecutionExample {
54  
55      public static void main(final String[] args) throws Exception {
56          final HttpRequester httpRequester = RequesterBootstrap.bootstrap()
57                  .setStreamListener(new Http1StreamListener() {
58  
59                      @Override
60                      public void onRequestHead(final HttpConnection connection, final HttpRequest request) {
61                          System.out.println(connection.getRemoteAddress() + " " + new RequestLine(request));
62  
63                      }
64  
65                      @Override
66                      public void onResponseHead(final HttpConnection connection, final HttpResponse response) {
67                          System.out.println(connection.getRemoteAddress() + " " + new StatusLine(response));
68                      }
69  
70                      @Override
71                      public void onExchangeComplete(final HttpConnection connection, final boolean keepAlive) {
72                          if (keepAlive) {
73                              System.out.println(connection.getRemoteAddress() + " exchange completed (connection kept alive)");
74                          } else {
75                              System.out.println(connection.getRemoteAddress() + " exchange completed (connection closed)");
76                          }
77                      }
78  
79                  })
80                  .setSocketConfig(SocketConfig.custom()
81                          .setSoTimeout(5, TimeUnit.SECONDS)
82                          .build())
83                  .create();
84  
85          final HttpCoreContext coreContext = HttpCoreContext.create();
86          final HttpHost target = new HttpHost("httpbin.org");
87          final String[] requestUris = new String[] {"/", "/ip", "/user-agent", "/headers"};
88  
89          for (int i = 0; i < requestUris.length; i++) {
90              final String requestUri = requestUris[i];
91              final ClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, target, requestUri);
92              try (ClassicHttpResponse response = httpRequester.execute(target, request, Timeout.ofSeconds(5), coreContext)) {
93                  System.out.println(requestUri + "->" + response.getCode());
94                  System.out.println(EntityUtils.toString(response.getEntity()));
95                  System.out.println("==============");
96              }
97          }
98      }
99  
100 }