To download the jar package import Java.io.fileoutputstream;import Java.io.ioexception;import Org.apache.http.httpentity;import Org.apache.http.httpresponse;import Org.apache.http.client.httpclient;import Org.apache.http.client.methods.httpget;import Org.apache.http.impl.client.defaulthttpclient;import org.apache.http.util.entityutils;/* * Simulation Download * class used: * 1.HttpClient: * 2.HttpResponse * 3.HttpEntity * 4.EntityUtils * */p Ublic class HttpDemo1 {public static void main (string[] args) throws IOException {//HttpClient: Client created. HttpClient client = new Defaulthttpclient (); Request Get:httpget String Path = "Http://www.baidu.com/img/bdlogo.gif"; HttpGet get = new HttpGet (path); Let the client execute the request. HttpResponse response = Client.execute (get); Data all in HttpResponse//1: Response code. int code = Response.getstatusline (). Getstatuscode (); if (code = = 200) {//Remove the returned data. The data is encapsulated into the Httpentity object. httpentity entity = response.getentity (); How to obtain data from a Httpentity object. Byte[] B = entityutils.tobytearray (entity); FileOutputStream fos = new FileOutputStream ("E:\\bb.gif"); Fos.write (b); Fos.flush (); Fos.close (); }}}import java.io.file;import Java.io.ioexception;import java.nio.charset.charset;import Org.apache.http.httpentity;import Org.apache.http.httpresponse;import Org.apache.http.client.clientprotocolexception;import Org.apache.http.client.httpclient;import Org.apache.http.client.methods.httppost;import Org.apache.http.entity.mime.formbodypart;import Org.apache.http.entity.mime.multipartentity;import Org.apache.http.entity.mime.content.filebody;import Org.apache.http.entity.mime.content.stringbody;import Org.apache.http.impl.client.defaulthttpclient;import org.apache.http.util.entityutils;/* * Classes Used: * HttpClient * httppost * filebody * formbodypart * multipartentity * HTTPRESP Onse * entityutils * */public class HttpDemo5 {public static void main (String[] args) throws Clientprotocolexception, IOException {//1: Create HttpClient object (create client) HttpClient client = New Defaulthttpclient (); 2: Create request method (Request in Web method) String uri = "Http://localhost:8080/FileUpload/FileUploadServlet"; HttpPost post = new HttpPost (URI); 3: Wrapping the data (file) to be sent//3.1: Gets the local file filename = new files ("E:\\aa.jpg"); 3.2: Create Filebody object (file body) filebody filebody = new Filebody (files); 3.3: Create Formbodypart Object (Form body section) Formbodypart part = new Formbodypart ("form", filebody); 4: Create the Multipartentity object. Multipartentity: multibody multipartentity entity = new multipartentity (); 5: Add the form body part to the multipart entity (add the data of the file type to the entity) Entity.addpart (parts); 5: Add plain Text data to multi-part entities Entity.addpart ("username", new stringbody ("haha", "text/html", Charset.forname ("Utf-8")); Entity.addpart ("Password", new Stringbody ("123")); 6: Set the requested entity Post.setEntity (entity); 7: Let the client execute the request (with data request), get the HttpResponse object (Response object) httpresponse response = Client.execute (POST); 8: Get response code by response Object INT code = Response.getstatusline (). Getstatuscode (); 9: If the response code is 200 (Success response code), get the data returned by the server if (code = = 200) {//9.1: Gets the Httpentity object (obtained through a response) httpentity en Tity2 = Response.getentity (); 10: Using the Entityutils tool class, convert the obtained data (entity) to a byte array form, and any file can be saved in bytes byte[] b = Entityutils.tobytearray (Entity2); Output content System.out.println (new String (b, "Utf-8")); } }}
"HttpClient" case study