Apache-httpclient-http-get-request

提供:Dev Guides
移動先:案内検索

Apache HttpClient-Http Getリクエスト

GETメソッドは、特定のURIを使用して特定のサーバーから情報を取得するために使用されます。 GETを使用するリクエストは、データを取得するだけで、データに他の影響を与えません。

HttpClient APIは、getリクエストメソッドを表す HttpGet という名前のクラスを提供します。

以下の手順に従って、HttpClientライブラリを使用してgetリクエストを送信します

ステップ1-HttpClientオブジェクトを作成する

*HttpClients* クラスの* createDefault()*メソッドは *CloseableHttpClient* オブジェクトを返します。これは *HttpClient* インターフェイスの基本実装です。

このメソッドを使用して、以下に示すようにHttpClientオブジェクトを作成します-

CloseableHttpClient httpclient = HttpClients.createDefault();

ステップ2-HttpGetオブジェクトを作成する

*HttpGet* クラスは、URIを使用して指定されたサーバーの情報を取得するHTTPGET要求を表します。

このクラスをインスタンス化して、HTTP GETリクエストを作成します。 このクラスのコンストラクターは、URIを表すString値を受け入れます。

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

ステップ3-Getリクエストを実行する

*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.finddevguides.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>