Http programming (2) using Apache API implementation, apacheapi

Source: Internet
Author: User

Http programming (2) using Apache API implementation, apacheapi

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;/** classes used for simulated download *: * 1. httpClient: * 2. httpResponse * 3. httpEntity * 4. entityUtils **/publi C class HttpDemo1 {public static void main (String [] args) throws IOException {// HttpClient: the client is 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.exe cute (get); // all data is in HttpResponse // 1: response code. Int code = response. getStatusLine (). getStatusCode (); if (code = 200) {// retrieve the returned data. Data is encapsulated into HttpEntity objects. HttpEntity entity = response. getEntity (); // how to obtain data in the 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;

/*
* Used classes:
* HttpClient
* HttpPost
* FileBody
* FormBodyPart
* MultipartEntity
* HttpResponse
* EntityUtils
*
*/

Public class HttpDemo5 {public static void main (String [] args) throws ClientProtocolException, IOException {// 1: Create an HttpClient object (create a client) HttpClient client = new DefaultHttpClient (); // 2: Create a request method (method in the web) String uri = "http: // localhost: 8080/FileUpload/FileUploadServlet"; HttpPost post = new HttpPost (uri ); // 3: Package the data (File) to be sent // 3.1: Obtain the local file File = new file ("e: \ aa.jpg"); // 3.2: create a FileBody object Body) FileBody fileBody = new FileBody (file); // 3.3: Create a FormBodyPart object (form body part) FormBodyPart part = new FormBodyPart ("form", fileBody); // 4: create a MultipartEntity object. MultipartEntity: multipart entity MultipartEntity entity = new MultipartEntity (); // 5: add the form body to the multipart entity (add file-type data to entity) entity. addPart (part); // 5: Add plain text data to the multi-part entity. addPart ("username", new StringBody ("Haha", "text/html", Charset. forName ("UTF-8"); entity. addPart ("password", new StringBody ("123"); // 6: Set the request object post. setEntity (entity); // 7: Let the client execute the request (a request with data) and get the HttpResponse object (response object) HttpResponse response = client.exe cute (post ); // 8: Get the response code int code = response through the response object. getStatusLine (). getStatusCode (); // 9: if the response code is 200 (success response code), obtain the data returned by the server if (code = 200) {// 9.1: get HttpEntity object (obtained through response) HttpEntity entity2 = response. getEntity (); // 10: Use the EntityUtils tool class to convert the obtained data (entity) to a byte array, any file can save byte [] B = EntityUtils in bytes. toByteArray (entity2); // output content System. out. println (new String (B, "UTF-8 "));}}}
 

 

 

 

Related Article

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.