Java Implementation HTTP connection (HTTPClient)

Source: Internet
Author: User

In the internship, the use of the HTTP connection, has been understood to be very vague, specifically to write an analysis of the article. Please advise me where the analysis is not in place.

HTTP is currently a generic version of HTTP 1.1.

HTTP connections are broadly divided into 2 common requests--get,post

I. Basic process (using httpclient)

get{

1. New HttpClient Object--HttpClient http = httpclients.createdefault ();

2. Create a GET request--httpget get = new HttpGet (url--interface address/request path);

3. Get the response--HttpResponse resp = Http.execute (get);

4. Get the response entity-- httpentity entity = resp.getentity ();

5. New Result object string--String result = entityutils.tostring (entity, "Utf-8");

6. Print-System.out.print ("The result is:" +result);

}

post{

1. New HttpClient Object--HttpClient http = httpclients.createdefault ();

2. Create POST request--httppost post = new HttpPost (url--interface address/request path);

3. Create a list container that holds the basic key-value pairs--{

list<basicnamevaluepair> params = new arraylist<> ();

Params.add (key value pair 1);

Params.add (key value pair 2);

}

4. Add the message entity--post.setentity (new urlencodedformentity (params, "utf-8") to the request;

5. Get the response-HttpResponse resp = Http.execute (post);

6. Get the response entity-- httpentity entity = resp.getentity ();

7. New Result Object-String result = entityutils.tostring (entity, "Utf-8");

8. Print-System.out.print ("The result is:" +result);

}

For the time being, it's such a thing.

Java Implementation HTTP connection (HTTPClient)

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.