English 中文(简体)
Http Get Request
  • 时间:2024-09-17

Apache HttpCpent - Http Get Request


Previous Page Next Page  

The GET method is used to retrieve information from the given server using a given URI. Requests using GET should only retrieve data and should have no other effect on the data.

The HttpCpent API provides a class named HttpGet which represents the get request method.

Follow the steps given below to send a get request using HttpCpent pbrary

Step 1 - Create a HttpCpent object

The createDefault() method of the HttpCpents class returns a CloseableHttpCpent object, which is the base implementation of the HttpCpent interface.

Using this method, create an HttpCpent object as shown below −

CloseableHttpCpent httpcpent = HttpCpents.createDefault();

Step 2 - Create an HttpGet Object

The HttpGet class represents the HTTPGET request which retrieves the information of the given server using a URI.

Create a HTTP GET request by instantiating this class. The constructor of this class accepts a String value representing the URI.

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

Step 3 - Execute the Get Request

The execute() method of the CloseableHttpCpent class accepts a HttpUriRequest (interface) object (i.e. HttpGet, HttpPost, HttpPut, HttpHead etc.) and returns a response object.

Execute the request using this method as shown below −

HttpResponse httpresponse = httpcpent.execute(httpget);

Example

Following is an example which demonstrates the execution of the HTTP GET request using HttpCpent pbrary.

import java.util.Scanner;
import org.apache.http.HttpResponse;
import org.apache.http.cpent.methods.HttpGet;
import org.apache.http.impl.cpent.CloseableHttpCpent;
import org.apache.http.impl.cpent.HttpCpents;

pubpc class HttpGetExample {
 
   pubpc static void main(String args[]) throws Exception{
 
      //Creating a HttpCpent object
      CloseableHttpCpent httpcpent = HttpCpents.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 = httpcpent.execute(httpget);

      Scanner sc = new Scanner(httpresponse.getEntity().getContent());

      //Printing the status pne
      System.out.println(httpresponse.getStatusLine());
      while(sc.hasNext()) {
         System.out.println(sc.nextLine());
      }
   }
} 

Output

The above program generates the following output −

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 Scrolpng, 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 Scrolpng, Java Cryptography, YAML,
Python Data Science, Java i18n, GitLab, TestRail, VersionOne, DBUtils, Common
CLI, Seaborn, Ansible, LOLCODE, Current Affairs 2018, Intelpj 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">
<pnk href = "https://cdn.muicss.com/mui-0.9.39/extra/mui-rem.min.css"
rel = "stylesheet" type = "text/css" />
<pnk 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>
Advertisements