HttpClient using Notes

Source: Internet
Author: User

Four Step walk:

1. Create an instance of Defaulthttpclient first

HttpClient httpclient=new defaulthttpclient ();

2. Send a GET request:

First create a HttpGet object, pass in the destination's network address, and then call the Execute () method of httpclient:

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

Httpclient.execute (HttpGet);

3. Return to Response entity ↓

HttpResponse response

4. Get the message entity

httpentity entity = response.getentity ();

Initial use of code

public class httpclientdemo{//HttpClient represents an HTTP client//Httpentity message carrier, sending or receiving a message, which can be obtained through a client request or a server response.  Httpconnection represents an HTTP connection/** * @param args */public static void main (string[] args) {//create default client instance HttpCLient httpCLient = new Defaulthttpclient ();//Create a GET request instance httpget HttpGet = new HttpGet ("http://www.baidu.com"); SYSTEM.OUT.PRINTLN ("Executing request" +httpget.geturi ()); The try{//client performs a GET request to return the response entity HttpResponse response = Httpclient.execute (HttpGet);//server Response status line System.out.println (Response.getstatusline ()); Header[] heads = response.getallheaders ();//Print all response headers for (header H:heads) {System.out.println (H.getname () + ":" + H.getvalue ());} Gets the response message entity httpentity entities = response.getentity (); System.out.println ("------------------------------------"), if (entity! = NULL) {//Response content System.out.println ( Entityutils.tostring (entity)); System.out.println ("----------------------------------------");//Response Content Length System.out.println ("Response content Length:" + Entity.getcontentlength ());}} catch (Clientprotocolexception e) {e.printstacktrace ();} CAtch (IOException e) {e.printstacktrace ();} Finally{httpclient.getconnectionmanager (). shutdown ();}}}

  

Resources

http://blog.csdn.net/manymore13/article/details/8481230

Http://itindex.net/detail/52566-httpclient

HttpClient using Notes

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.