Apache HttpClient - Http Post 请求

  • 简述

    POST 请求用于向服务器发送数据;例如,客户信息、文件 上传等,使用 HTML 表单。
    HttpClient API 提供了一个名为 HttpPost 的类,它代表 POST 请求。
    按照下面给出的步骤使用 HttpClient 库发送 HTTP POST 请求。
  • 第 1 步 - 创建一个 HttpClient 对象

    HttpClients类的createDefault()方法返回一个类的对象CloseableHttpClient,它是 HttpClient 接口的基本实现。
    使用此方法,创建一个 HttpClient 对象。
    
    CloseableHttpClient httpClient = HttpClients.createDefault();
    
  • 第 2 步 - 创建 HttpPost 对象

    HttpPost 类表示 HTTP POST 请求。这会发送所需的数据并使用 URI 检索给定服务器的信息。
    通过实例化 HttpPost 类来创建此请求,并将表示 URI 的字符串值作为参数传递给其构造函数。
    
    HttpGet httpGet = new HttpGet("http://www.cainiaoya.com/");
    
  • 第 3 步 - 执行获取请求

    CloseableHttpClient 对象的 execute() 方法接受一个 HttpUriRequest(接口)对象(即 HttpGet、HttpPost、HttpPut、HttpHead 等)并返回一个响应对象。< /div>
    
    HttpResponse httpResponse = httpclient.execute(httpget);
    
  • 示例

    下面是一个例子,它演示了使用 HTTP POST 请求的执行 HttpClient 库。
    
    import org.apache.http.HttpResponse;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    public class HttpPostExample {
     
       public static void main(String args[]) throws Exception{
     
          //Creating a HttpClient object
          CloseableHttpClient httpclient = HttpClients.createDefault();
          //Creating a HttpGet object
          HttpPost httppost = new HttpPost("https://www.cainiaoya.com/");
          //Printing the method used
          System.out.println("Request Type: "+httppost.getMethod());
          //Executing the Get request
          HttpResponse httpresponse = httpclient.execute(httppost);
          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: POST
    <!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" conten t= "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>