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  package org.apache.hc.client5.http.examples;
28  
29  import java.util.ArrayList;
30  import java.util.List;
31  
32  import org.apache.hc.client5.http.classic.methods.HttpGet;
33  import org.apache.hc.client5.http.classic.methods.HttpPost;
34  import org.apache.hc.client5.http.entity.UrlEncodedFormEntity;
35  import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
36  import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
37  import org.apache.hc.client5.http.impl.classic.HttpClients;
38  import org.apache.hc.core5.http.HttpEntity;
39  import org.apache.hc.core5.http.NameValuePair;
40  import org.apache.hc.core5.http.io.entity.EntityUtils;
41  import org.apache.hc.core5.http.message.BasicNameValuePair;
42  
43  public class QuickStart {
44  
45      public static void main(final String[] args) throws Exception {
46          try (final CloseableHttpClient httpclient = HttpClients.createDefault()) {
47              final HttpGet httpGet = new HttpGet("http://httpbin.org/get");
48              // The underlying HTTP connection is still held by the response object
49              // to allow the response content to be streamed directly from the network socket.
50              // In order to ensure correct deallocation of system resources
51              // the user MUST call CloseableHttpResponse#close() from a finally clause.
52              // Please note that if response content is not fully consumed the underlying
53              // connection cannot be safely re-used and will be shut down and discarded
54              // by the connection manager.
55              try (final CloseableHttpResponse response1 = httpclient.execute(httpGet)) {
56                  System.out.println(response1.getCode() + " " + response1.getReasonPhrase());
57                  final HttpEntity entity1 = response1.getEntity();
58                  // do something useful with the response body
59                  // and ensure it is fully consumed
60                  EntityUtils.consume(entity1);
61              }
62  
63              final HttpPost httpPost = new HttpPost("http://httpbin.org/post");
64              final List<NameValuePair> nvps = new ArrayList<>();
65              nvps.add(new BasicNameValuePair("username", "vip"));
66              nvps.add(new BasicNameValuePair("password", "secret"));
67              httpPost.setEntity(new UrlEncodedFormEntity(nvps));
68  
69              try (final CloseableHttpResponse response2 = httpclient.execute(httpPost)) {
70                  System.out.println(response2.getCode() + " " + response2.getReasonPhrase());
71                  final HttpEntity entity2 = response2.getEntity();
72                  // do something useful with the response body
73                  // and ensure it is fully consumed
74                  EntityUtils.consume(entity2);
75              }
76          }
77      }
78  
79  }