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