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