Apache HttpClient - Http Get 请求

  • 简述

    GET 方法用于使用给定的 URI 从给定的服务器检索信息。使用 GET 的请求应该只检索数据,并且对数据没有其他影响。
    HttpClient API 提供了一个名为 HttpGet 的类,代表获取请求方法。
    按照下面给出的步骤使用 HttpClient 库发送 get 请求
  • 第 一 步 - 创建一个 HttpClient 对象

    HttpClients 类的 createDefault() 方法返回一个 CloseableHttpClient 对象,它是 HttpClient 接口的基本实现。
    使用这个方法,创建一个HttpClient对象,如下图-
    
    CloseableHttpClient httpclient = HttpClients.createDefault();
    
  • 第二步 - 创建一个 HttpGet 对象

    HttpGet 类表示使用 URI 检索给定服务器信息的 HTTPGET 请求。
    通过实例化这个类来创建一个 HTTP GET 请求。这个类的构造函数接受 表示 URI 的字符串值。
    
    HttpGet httpget = new HttpGet("http://www.jc2182.com/");
    
  • 第三步 - 执行获取请求

    CloseableHttpClient 类的 execute() 方法接受一个 HttpUriRequest (接口)对象(即 HttpGet、HttpPost、HttpPut、HttpHead 等)并返回响应 对象。
    使用该方法执行请求,如下图-
    
    HttpResponse httpresponse = httpclient.execute(httpget);
    
  • 示例

    下面是一个例子,演示了使用 HttpClient 库执行 HTTP GET 请求。
    
    import java.util.Scanner;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    public class HttpGetExample {
     
       public static void main(String args[]) throws Exception{
     
          //Creating a HttpClient object
          CloseableHttpClient httpclient = HttpClients.createDefault();
          //Creating a HttpGet object
          HttpGet httpget = new HttpGet("https://www.jc2182.com/ ");
          //Printing the method used
          System.out.println("Request Type: "+httpget.getMethod());
          //Executing the Get request
          HttpResponse httpresponse = httpclient.execute(httpget);
          Scanner sc = new Scanner(httpresponse.getEntity().getContent());
          //Printing the status line
          System.out.println(httpresponse.getStatusLine());
          while(sc.hasNext()) {
             System.out.println(sc.nextLine());
          }
       }
    } 
    
  • 输出

    上述程序生成以下输出 -
    
    Request Type: GET
    <!DOCTYPE html>
    <!--[if IE 8]><html class = "ie ie8"> <![endif]-->
    <!--[if IE 9]><html class = "ie ie9"> <![endif]-->
    <!--[if gt IE 9]><!-->
    <html lang = "en-US"> <!--<![endif]-->
    <head>
    <!-- Basic -->
    <meta charset = "utf-8">
    <title>Parallax Scrolling, Java Cryptography, YAML, Python Data Science, Java
    i18n, GitLab, TestRail, VersionOne, DBUtils, Common CLI, Seaborn, Ansible,
    LOLCODE, Current Affairs 2018, Apache Commons Collections</title>
    <meta name = "Description" content = "Parallax Scrolling, Java Cryptography, YAML,
    Python Data Science, Java i18n, GitLab, TestRail, VersionOne, DBUtils, Common
    CLI, Seaborn, Ansible, LOLCODE, Current Affairs 2018, Intellij Idea, Apache
    Commons Collections, Java 9, GSON, TestLink, Inter Process Communication (IPC),
    Logo, PySpark, Google Tag Manager, Free IFSC Code, SAP Workflow"/>
    <meta name = "Keywords" content = "Python Data Science, Java i18n, GitLab,
    TestRail, VersionOne, DBUtils, Common CLI, Seaborn, Ansible, LOLCODE, Gson,
    TestLink, Inter Process Communication (IPC), Logo"/>
    <meta http-equiv = "X-UA-Compatible" content = "IE = edge">
    <meta name = "viewport" content = "width = device-width,initial-scale = 1.0,userscalable = yes">
    <link href = "https://cdn.muicss.com/mui-0.9.39/extra/mui-rem.min.css"
    rel = "stylesheet" type = "text/css" />
    <link rel = "stylesheet" href="/questions/css/home.css?v = 3" />
    <script src = "/questions/js/jquery.min.js"></script>
    <script src = "/questions/js/fontawesome.js"></script>
    <script src = "https://cdn.muicss.com/mui-0.9.39/js/mui.min.js"></script>
    </head>
    . . . . . . . . . . . . . . . . . . . . . . . .
    . . . . . . . . . . . . . . . . . . . . . . . .
    . . . . . . . . . . . . . . . . . . . . . . . .
    . . . . . . . . . . . . . . . . . . . . . . . .
    </script>
    </body>
    </html>