HttpClient4.3.1 Simple Getting Started instance turn

Source: Internet
Author: User

Public class Httpclienttest {

  • public static void Main (String args[]) {
  • //Create Httpclientbuilder
  • Httpclientbuilder Httpclientbuilder = Httpclientbuilder.create ();
  • //httpclient
  • Closeablehttpclient closeablehttpclient = Httpclientbuilder.build ();
  • HttpGet httpget = new HttpGet ("http://www.gxnu.edu.cn/default.html");
  • System.out.println (Httpget.getrequestline ());
  • try {
  • //Execute GET request
  • HttpResponse HttpResponse = Closeablehttpclient.execute (HttpGet);
  • //Get Response message entity
  • httpentity entity = httpresponse.getentity ();
  • //Response status
  • System.out.println ("Status:" + Httpresponse.getstatusline ());
  • //Determine if the response entity is empty
  • if (Entity! = null) {
  • System.out.println ("contentencoding:" + entity.getcontentencoding ());
  • SYSTEM.OUT.PRINTLN ("Response content:" + entityutils.tostring (entity));
  • }
  • } catch (IOException e) {
  • E.printstacktrace ();
  • } finally {
  • try {
public class Httpclienttest {public static void main (String args[]) {//create Httpclientbuilder HTTPCLIENTBU        Ilder Httpclientbuilder = Httpclientbuilder.create ();        HttpClient closeablehttpclient closeablehttpclient = Httpclientbuilder.build ();        HttpGet httpget = new HttpGet ("http://www.gxnu.edu.cn/default.html");        System.out.println (Httpget.getrequestline ());            try {//execute GET request HttpResponse HttpResponse = Closeablehttpclient.execute (HttpGet);            Gets the response message entity httpentity entities = httpresponse.getentity ();            Response Status System.out.println ("Status:" + Httpresponse.getstatusline ()); Determines whether the response entity is empty if (entity! = null) {System.out.println ("contentencoding:" + Entity.getcontentenco                Ding ());            SYSTEM.OUT.PRINTLN ("Response content:" + entityutils.tostring (entity));        }} catch (IOException e) {e.printstacktrace (); } Finally {try { 
[Java]View Plaincopyprint?
    1. //Close stream and release resources
    2. Closeablehttpclient.close ();
    3. } catch (IOException e) {
    4. E.printstacktrace ();
    5. }
    6. }
    7. }
                Closes the stream and frees the resource                closeablehttpclient.close ();            } catch (IOException e) {                e.printstacktrace ();}        }}    }

The following is from the HttpClient4.3.1 API Documentation: http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/ Overview-summary.html

2, Httpclientbuilder

Httpclientbuilder is used to create closeablehttpclient instances. Look at the API documentation, Abstracthttpclient, autoretryhttpclient, defaulthttpclient, etc. are deprecated, using httpclientbuilder instead.

3, Closeablehttpclient

Implementation interface: Closeable, autocloseable, HttpClient; sub-class: Abstracthttpclient

4, HttpGet

Non-thread safe; HttpGet has three constructor methods: HttpGet (), HttpGet (String uri), HttpGet (URI Uri)

5, HttpResponse

The server returns an HTTP response message after receiving and interpreting the request

     Response      = Status-line                     * ((General-header                      | Response-header                      | entity-header) CRLF)                     CRLF                     [ Message-body]

HttpClient4.3.1 Simple Getting Started instance turn

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.