Apache HttpClient - Http 獲取請求



GET 方法用於使用給定的 URI 從給定的伺服器檢索資訊。使用 GET 的請求應只檢索資料,不得對資料產生任何其他影響。

HttpClient API 提供了一個名為 HttpGet 的類,它表示 get 請求方法。

按照以下步驟傳送使用 HttpClient 庫的 get 請求

步驟 1 - 建立一個 HttpClient 物件

HttpClients 類的 createDefault() 方法返回一個 CloseableHttpClient 物件,它就是 HttpClient 介面的基本實現。

使用此方法,如下所示建立 HttpClient 物件 −

CloseableHttpClient httpclient = HttpClients.createDefault();

步驟 2 - 建立一個 HttpGet 物件

HttpGet 類表示 HTTPGET 請求,它使用 URI 檢索給定伺服器的資訊。

透過例項化此類建立 HTTP GET 請求。此類的建構函式接受一個表示 URI 的字串值。

HttpGet httpget = new HttpGet("https://tutorialspoint.tw/");

步驟 3 - 執行獲取請求

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://tutorialspoint.tw/ ");

      //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>
廣告