Detailed explanation of Apache HttpClient usage

I don’t need to say more about the importance of the Http protocol. Compared with the URLConnection that comes with the traditional JDK, HttpClient has increased ease of use and flexibility (the specific differences will be discussed in the future). It is easy and convenient for developers to test the interface (based on the Http protocol), which not only improves the efficiency of development, but also facilitates the robustness of the code. Therefore, mastering HttpClient is a very important compulsory content. After mastering HttpClient, I believe that the understanding of Http protocol will be more in-depth.

 

1. Introduction

HttpClient is a sub-project under Apache Jakarta Common, which is used to provide an efficient, up-to-date, feature-rich client programming toolkit that supports the HTTP protocol, and it supports the latest version and recommendations of the HTTP protocol. HttpClient has been used in many projects, such as Cactus and HTMLUnit, two other well-known open source projects on Apache Jakarta, both use HttpClient.

 

2. Characteristics

1. Based on standard, pure java language. Implemented Http1.0 and Http1.1

2. Implements all Http methods (GET, POST, PUT, DELETE, HEAD, OPTIONS, and TRACE) in an extensible object-oriented structure.

3. Support HTTPS protocol.

4. Establish a transparent connection through Http proxy.

5. Use the CONNECT method to establish a tunneled https connection through the Http proxy.

6. Basic, Digest, NTLMv1, NTLMv2, NTLM2 Session, SNPNEGO/Kerberos authentication scheme.

7. Plug-in custom authentication scheme.

8. Portable and reliable socket factory makes it easier to use third-party solutions.

9. The connection manager supports multi-threaded applications. Supports setting the maximum number of connections, and supports setting the maximum number of connections per host, and discovers and closes expired connections.

10. Automatically handle cookies in Set-Cookie.

11. Plug-in custom cookie policy.

12. The output stream of Request can avoid direct buffering of the content in the stream to the socket server.

13. The input stream of Response can effectively read the corresponding content directly from the socket server.

14. Use KeepAlive to maintain persistent connections in http1.0 and http1.1.

15. Get the response code and headers sent by the server directly.

16. Ability to set connection timeout.

17. Experimental support for http1.1 response caching.

18. The source code is freely available under the Apache License

 

3. How to use

       Mavn coordinates

<dependency>
	<groupId>org.apache.httpcomponents</groupId>
	<artifactId>httpclient</artifactId>
	<version>4.3.4</version>
</dependency>

 

Using HttpClient to send requests and receive responses is very simple. Generally, the following steps are required.

1. Create an HttpClient object.

2. Create an instance of the request method and specify the request URL. If you need to send a GET request, create an HttpGet object; if you need to send a POST request, create an HttpPost object.

3. If you need to send request parameters, you can call the common setParams(HetpParams params) method of HttpGet and HttpPost to add request parameters; for HttpPost objects, you can also call the setEntity(HttpEntity entity) method to set request parameters.

4. Call execute(HttpUriRequest request) of the HttpClient object to send the request, and this method returns an HttpResponse.

5. Call HttpResponse's getAllHeaders(), getHeaders(String name) and other methods to obtain the server's response headers; call HttpResponse's getEntity() method to obtain the HttpEntity object, which wraps the server's response content. The program can obtain the response content of the server through this object.

6. Release the connection. The connection must be released regardless of the success of executing the method

 

 

4. Example of post and get request
package com.ickes;

import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

public class HttpClientDemo {
    
	public static void main(String[] args) throws Exception  { 
		get();
	}
	
	/**
	 * post方式提交json代码
	 * @throws Exception 
	 */
	public static void postJson() throws Exception{
		//创建默认的httpClient实例. 
		CloseableHttpClient httpclient = null;
		//接收响应结果
		CloseableHttpResponse response = null;
		try {
			//创建httppost
			httpclient = HttpClients.createDefault();  
			String url ="http://192.168.16.36:8081/goSearch/gosuncn/deleteDocs.htm";
			HttpPost httpPost = new HttpPost(url);
			httpPost.addHeader(HTTP.CONTENT_TYPE,"application/x-www-form-urlencoded");
			//参数
			String json ="{'ids':['html1','html2']}";
			StringEntity se = new StringEntity(json);
			se.setContentEncoding("UTF-8");
			se.setContentType("application/json");//发送json需要设置contentType
			httpPost.setEntity(se);
			response = httpclient.execute(httpPost);
			//解析返结果
			HttpEntity entity = response.getEntity(); 
			if(entity != null){
				String resStr = EntityUtils.toString(entity, "UTF-8");    
			    System.out.println(resStr);
			}
		} catch (Exception e) {
			throw e;
		}finally{
			httpclient.close();
		    response.close();
		}
	}
	
	 /** 
     * post方式提交表单(模拟用户登录请求) 
	 * @throws Exception 
     */  
    public static void postForm() throws Exception  {  
        // 创建默认的httpClient实例.    
		CloseableHttpClient httpclient = null;
		//发送请求
		CloseableHttpResponse response = null;
		try {
			httpclient = HttpClients.createDefault();  
			// 创建httppost    
			String url= "http://localhost:8080/search/ajx/user.htm";
			HttpPost httppost = new HttpPost(url);  
			// 创建参数队列    
			List<NameValuePair> formparams = new ArrayList<NameValuePair>();  
			formparams.add(new BasicNameValuePair("username", "admin"));  
			formparams.add(new BasicNameValuePair("password", "123456"));
			//参数转码
			UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");  
			httppost.setEntity(uefEntity); 
			response = httpclient.execute(httppost);  
			HttpEntity entity = response.getEntity();  
			if (entity != null) {  
			      System.out.println(EntityUtils.toString(entity, "UTF-8"));  
			}  
			//释放连接
		} catch (Exception e) {
			throw e;
		}finally{
			 httpclient.close();
		     response.close();
		}
    }  
    
    /** 
     * 发送 get请求 
     * @throws Exception 
     */  
    public static void get() throws Exception {  
    	CloseableHttpClient httpclient = null;
    	CloseableHttpResponse response = null;
        try {
        	httpclient = HttpClients.createDefault();  
			// 创建httpget.    
			HttpGet httpget = new HttpGet("http://www.baidu.com/");  
			// 执行get请求.    
			response = httpclient.execute(httpget);  
			// 获取响应实体    
			HttpEntity entity = response.getEntity();  
      
			// 打印响应状态    
			System.out.println(response.getStatusLine().getStatusCode());  
			if (entity != null) {  
			    // 打印响应内容    
			    System.out.println("Response content: " + EntityUtils.toString(entity));  
			}
		} catch (Exception e) {
			throw e;
		}finally{
			httpclient.close();
			response.close();
		}
    }
}

 

 

五、SSL跟上传文件实例

 

package com.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.util.ArrayList;
import java.util.List;
import javax.net.ssl.SSLContext;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.junit.Test;

public class HttpClientTest {

	@Test
	public void jUnitTest() {
		ssl();
	}

	/**
	 * HttpClient连接SSL
	 */
	public void ssl() {
		CloseableHttpClient httpclient = null;
		try {
			KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
			FileInputStream instream = new FileInputStream(new File("d:\\tomcat.keystore"));
			try {
				// 加载keyStore d:\\tomcat.keystore  
				trustStore.load(instream, "123456".toCharArray());
			} catch (CertificateException e) {
				e.printStackTrace();
			} finally {
				try {
					instream.close();
				} catch (Exception ignore) {
				}
			}
			// 相信自己的CA和所有自签名的证书
			SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(trustStore, new TrustSelfSignedStrategy()).build();
			// 只允许使用TLSv1协议
			SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null,
					SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
			httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
			// 创建http请求(get方式)
			HttpGet httpget = new HttpGet("https://localhost:8443/myDemo/Ajax/serivceJ.action");
			System.out.println("executing request" + httpget.getRequestLine());
			CloseableHttpResponse response = httpclient.execute(httpget);
			try {
				HttpEntity entity = response.getEntity();
				System.out.println("----------------------------------------");
				System.out.println(response.getStatusLine());
				if (entity != null) {
					System.out.println("Response content length: " + entity.getContentLength());
					System.out.println(EntityUtils.toString(entity));
					EntityUtils.consume(entity);
				}
			} finally {
				response.close();
			}
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (KeyManagementException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (KeyStoreException e) {
			e.printStackTrace();
		} finally {
			if (httpclient != null) {
				try {
					httpclient.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 上传文件
	 */
	public void upload() {
		CloseableHttpClient httpclient = HttpClients.createDefault();
		try {
			HttpPost httppost = new HttpPost("http://localhost:8080/myDemo/Ajax/serivceFile.action");

			FileBody bin = new FileBody(new File("F:\\image\\sendpix0.jpg"));
			StringBody comment = new StringBody("A binary file of some kind", ContentType.TEXT_PLAIN);

			HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("bin", bin).addPart("comment", comment).build();

			httppost.setEntity(reqEntity);

			System.out.println("executing request " + httppost.getRequestLine());
			CloseableHttpResponse response = httpclient.execute(httppost);
			try {
				System.out.println("----------------------------------------");
				System.out.println(response.getStatusLine());
				HttpEntity resEntity = response.getEntity();
				if (resEntity != null) {
					System.out.println("Response content length: " + resEntity.getContentLength());
				}
				EntityUtils.consume(resEntity);
			} finally {
				response.close();
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

   本实例是采用HttpClient4.3最新版本。该版本与之前的代码写法风格相差较大,大家多留意下。

 

参考:http://blog.csdn.net/wangpeng047/article/details/19624529

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326360817&siteId=291194637