Procedure: 1. Create an httpget (or httppost) object and pass the requested URL to the httpget (or httppost) object through the constructor; 2. Use the execute method of the defaulthttpclient class to send an http get or http post request and return the httpresponse object; 3. Return the response information through the getentity method of the httpresponse interface.
HTTP connection POST request
// Step 1: Create an httppost object <br/> httppost = new httppost (URL ); <br/> // set http post Request Parameters Using namevaluepair objects <br/> List <namevaluepair> Params = new arraylist <namevaluepair> (); <br/> Params. add (New basicnamevaluepair ("bookname", "2465158248"); <br/> system. out. println ("result1"); <br/> // set the httppost request parameter <br/> try <br/>{< br/> httppost. setentity (New urlencodedformentity (Params, HTTP. utf_8); <br/> // Step 2: Use the execute method to send an http get request and return the httpresponse object <br/> httpresponse; <br/> try <br/> {<br/> httpresponse = new defaulthttpclient(.exe cute (httppost); <br/> system. out. println ("result"); <br/> If (httpresponse. getstatusline (). getstatuscode () = 200) <br/>{< br/> // Step 3: Use the getentity method to return results. <br/> string result = entityutils. tostring (httpresponse. getentity (); <br/> system. out. println ("result" + result); <br/>}< br/> catch (clientprotocolexception E) <br/>{< br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>}< br/> catch (ioexception E) <br/>{< br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>}< br/> catch (unsupportedencodingexception e) <br/>{< br/> E. printstacktrace (); <br/>}< br/>}
HTTP connection GET request
String URL; <br/> // Step 1: Create an httpget object <br/> httpget = new httpget (URL); <br/> // step 2, use the execute method to send an http get request and return the httpresponse object <br/> httpresponse = new defaulthttpclient(cmd.exe cute (httpget); <br/> If (httpresponse. getstatusline (). getstatuscode () = 200) <br/>{< br/> // Step 3: Use the getentity method to return results. <br/> string result = entityutils. tostring (httpresponse. getentity (); <br/>}