Apache HttpClient - Demande Http Get

La méthode GET est utilisée pour récupérer des informations du serveur donné en utilisant un URI donné. Les requêtes utilisant GET ne doivent récupérer que des données et ne doivent avoir aucun autre effet sur les données.

L'API HttpClient fournit une classe nommée HttpGet qui représente la méthode de requête get.

Suivez les étapes ci-dessous pour envoyer une demande d'obtention à l'aide de la bibliothèque HttpClient

Étape 1 - Créer un objet HttpClient

le createDefault() méthode de la HttpClients classe renvoie un CloseableHttpClient object, qui est l'implémentation de base du HttpClient interface.

En utilisant cette méthode, créez un objet HttpClient comme indiqué ci-dessous -

CloseableHttpClient httpclient = HttpClients.createDefault();

Étape 2 - Créer un objet HttpGet

le HttpGet classe représente la requête HTTPGET qui récupère les informations du serveur donné à l'aide d'un URI.

Créez une requête HTTP GET en instanciant cette classe. Le constructeur de cette classe accepte une valeur String représentant l'URI.

HttpGet httpget = new HttpGet("http://www.tutorialspoint.com/");

Étape 3 - Exécutez la demande d'obtention

le execute() méthode de la CloseableHttpClient La classe accepte un objet HttpUriRequest (interface) (c'est-à-dire HttpGet, HttpPost, HttpPut, HttpHead, etc.) et renvoie un objet de réponse.

Exécutez la demande en utilisant cette méthode comme indiqué ci-dessous -

HttpResponse httpresponse = httpclient.execute(httpget);

Exemple

Voici un exemple qui illustre l'exécution de la requête HTTP GET à l'aide de la bibliothèque HttpClient.

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.tutorialspoint.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());
      }
   }
}

Production

Le programme ci-dessus génère la sortie suivante -

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>