HttpClient How to send a GET request

Source: Internet
Author: User

Import org.apache.http.HttpEntity;
Import Org.apache.http.client.methods.HttpGet;
Import org.apache.http.impl.client.CloseableHttpClient;
Import org.apache.http.impl.client.HttpClients;
Import Org.apache.http.util.EntityUtils;

Import java.io.IOException;

/**
* Created by Administrator on 2018/4/11.
*/
public class Httpclienttest {


public static void Main (string[] args) {
try {

String charset = "UTF-8";
Create Closeablehttpclient
Closeablehttpclient httpClient = Httpclients.custom (). build ();
String url = "Http://www.baidu.com";

If there are parameters, directly after the URL plus the number stitching such as http://www.baidu.com?a=n&b=n

Request method
HttpGet get = new HttpGet (URL);
Receive Request return object
Httpentity entity =httpclient.execute (GET). GetEntity ();
Use Entityutils to read the httpentity request return stream into a string
String result = entityutils.tostring (Entity,charset);
SYSTEM.OUT.PRINTLN (result);

Entityutils.consume (entity);
Httpclient.close ();

} catch (IOException e) {
E.printstacktrace ();
}

}

}

HttpClient How to send a GET request

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.