java之HttpClient簡單使用


最近在做微信公眾號開發,需要用java代碼訪問微信端接口來請求數據。

由於博主java的網絡通信也不是很精通,只是粗略了解皮毛,等以后知識上來了再深入研究java的網絡編程。

所以這篇文章就先介紹簡單使用。

需要的jar包:

<dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.3.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.4</version>
        </dependency>

 

本篇博客分為幾塊部分:http的get、post請求,https的get、post請求

新建一個類HttpUtil:

public class HttpUtil {
	private static PoolingHttpClientConnectionManager connMgr;
	private static RequestConfig requestConfig;
	private static final int MAX_TIMEOUT = 7000;

	static {
		// 設置連接池
		connMgr = new PoolingHttpClientConnectionManager();
		// 設置連接池大小
		connMgr.setMaxTotal(100);
		connMgr.setDefaultMaxPerRoute(connMgr.getMaxTotal());

		RequestConfig.Builder configBuilder = RequestConfig.custom();
		// 設置連接超時
		configBuilder.setConnectTimeout(MAX_TIMEOUT);
		// 設置讀取超時
		configBuilder.setSocketTimeout(MAX_TIMEOUT);
		// 設置從連接池獲取連接實例的超時
		configBuilder.setConnectionRequestTimeout(MAX_TIMEOUT);
		// 在提交請求之前 測試連接是否可用
		configBuilder.setStaleConnectionCheckEnabled(true);
		requestConfig = configBuilder.build();
	}
}

  

http部分

get請求:

/**
	 * 發送 GET 請求(HTTP),不帶輸入數據
	 * 
	 * @param url
	 * @return
	 */
	public static String doGet(String url) {
		return doGet(url, new HashMap<String, Object>());
	}

	/**
	 * 發送 GET 請求(HTTP),K-V形式
	 * 
	 * @param url
	 * @param params
	 * @return
	 */
	public static String doGet(String url, Map<String, Object> params) {
		String apiUrl = url;
		StringBuffer param = new StringBuffer();
		//拼接get請求參數
		int i = 0;
		for (String key : params.keySet()) {
			if (i == 0)
				param.append("?");
			else
				param.append("&");
			param.append(key).append("=").append(params.get(key));
			i++;
		}
		//往url后面添加所有的請求參數
		apiUrl += param;
		String result = null;
		//開始執行get請求,新建一個DefaultHttpClient對象
		HttpClient httpclient = new DefaultHttpClient();
		try {
			//新建一個處理特定url的HttpGet對象
			HttpGet httpGet = new HttpGet(apiUrl);
			//執行請求,得到response對象
			HttpResponse response = httpclient.execute(httpGet);
			//獲取響應狀態碼
			int statusCode = response.getStatusLine().getStatusCode();

			System.out.println("執行狀態碼 : " + statusCode);
			
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				//處理結果
				// result = EntityUtils.toString(entity, "UTF-8");
				InputStream instream = entity.getContent();
				result = inputStream2String(instream, "UTF-8");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}

  inputStream2String:

private static String inputStream2String(InputStream is, String encode) {
		String result = "";
		try {
			if (encode == null || encode.equals("")) {
				// 默認以utf-8形式
				encode = "utf-8";
			}
			BufferedReader reader = new BufferedReader(new InputStreamReader(is, encode));
			StringBuffer sb = new StringBuffer();

			while ((result = reader.readLine()) != null) {
				sb.append(result).append("\n");
			}
			return sb.toString();
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return result;
	}

  post請求:

/**
	 * 發送 POST 請求(HTTP),不帶輸入數據
	 * 
	 * @param apiUrl
	 * @return
	 */
	public static String doPost(String apiUrl) {
		return doPost(apiUrl, new HashMap<String, Object>());
	}

	/**
	 * 發送 POST 請求(HTTP),K-V形式
	 * 
	 * @param apiUrl
	 *            API接口URL
	 * @param params
	 *            參數map
	 * @return
	 */
	public static String doPost(String apiUrl, Map<String, Object> params) {
		//新建一個可以關閉的httpClient對象
		CloseableHttpClient httpClient = HttpClients.createDefault();
		String httpStr = null;
		//新建一個處理特定請求的httpPost對象
		HttpPost httpPost = new HttpPost(apiUrl);
		//可以關閉的HttpResponse
		CloseableHttpResponse response = null;

		try {
			//post請求設置
			httpPost.setConfig(requestConfig);
			//參數鍵值對對象(NameValuePair)的List
			List<NameValuePair> pairList = new ArrayList<NameValuePair>(params.size());
			for (Map.Entry<String, Object> entry : params.entrySet()) {
				NameValuePair pair = new BasicNameValuePair(entry.getKey(), entry.getValue().toString());
				pairList.add(pair);
			}
			//設置請求實體(參數)
			httpPost.setEntity(new UrlEncodedFormEntity(pairList, Charset.forName("UTF-8")));
			//執行請求
			response = httpClient.execute(httpPost);
			System.out.println(response.toString());
			//獲取響應實體
			HttpEntity entity = response.getEntity();
			//將響應實體以字符串形式變成結果
			httpStr = EntityUtils.toString(entity, "UTF-8");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (response != null) {
				try {
					//關閉流
					EntityUtils.consume(response.getEntity());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return httpStr;
	}

	/**
	 * 發送 POST 請求(HTTP),JSON形式
	 * 
	 * @param apiUrl
	 * @param json
	 *            json對象
	 * @return
	 */
	public static String doPost(String apiUrl, Object json) {
		CloseableHttpClient httpClient = HttpClients.createDefault();
		String httpStr = null;
		HttpPost httpPost = new HttpPost(apiUrl);
		CloseableHttpResponse response = null;
		//大體過程和上面類似
		try {
			httpPost.setConfig(requestConfig);
			StringEntity stringEntity = new StringEntity(json.toString(), "UTF-8");// 解決中文亂碼問題
			stringEntity.setContentEncoding("UTF-8");
			//內容類型設置成json格式
			stringEntity.setContentType("application/json");
			httpPost.setEntity(stringEntity);
			response = httpClient.execute(httpPost);
			HttpEntity entity = response.getEntity();
			System.out.println(response.getStatusLine().getStatusCode());
			httpStr = EntityUtils.toString(entity, "UTF-8");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (response != null) {
				try {
					EntityUtils.consume(response.getEntity());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return httpStr;
	}

  

HTTPS

我新建了一個類SSLClient:

import java.security.cert.CertificateException;  
import java.security.cert.X509Certificate;  
import javax.net.ssl.SSLContext;  
import javax.net.ssl.TrustManager;  
import javax.net.ssl.X509TrustManager;

import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;  
import org.apache.http.conn.scheme.SchemeRegistry;  
import org.apache.http.conn.ssl.SSLSocketFactory;  
import org.apache.http.impl.client.DefaultHttpClient;  
//用於進行Https請求的HttpClient  
public class SSLClient extends DefaultHttpClient{  
    public SSLClient() throws Exception{  
        super();
        SSLContext ctx = SSLContext.getInstance("TLS");  
        X509TrustManager tm = new X509TrustManager() {  
                public void checkClientTrusted(X509Certificate[] chain,  
                        String authType) throws CertificateException {  
                }  
                public void checkServerTrusted(X509Certificate[] chain,  
                        String authType) throws CertificateException {  
                }  
                public X509Certificate[] getAcceptedIssuers() {  
                    return null;  
                }  
        };
        ctx.init(null, new TrustManager[]{tm}, null);
        //當前對象的管理
        ClientConnectionManager ccm = this.getConnectionManager();
        SchemeRegistry sr = ccm.getSchemeRegistry();//當前管理者的注冊方案
        //SSL工廠
        SSLSocketFactory ssf = new SSLSocketFactory(ctx,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        sr.register(new Scheme("https", 443, ssf));//注冊SSL
    }  
}  

  get請求:

/**
	 * 發送get請求
	 * 
	 * @param url
	 *            鏈接地址
	 * @param charset
	 *            字符編碼,若為null則默認utf-8
	 * @return
	 */
	public static String doGetSSL(String url, String charset) {
		if (null == charset) {
			charset = "utf-8";
		}
		HttpClient httpClient = null;
		HttpGet httpGet = null;
		String result = null;

		try {
			httpClient = new SSLClient();
			httpGet = new HttpGet(url);

			HttpResponse response = httpClient.execute(httpGet);
			if (response != null) {
				HttpEntity resEntity = response.getEntity();
				if (resEntity != null) {
					result = EntityUtils.toString(resEntity, charset);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

  post請求:

/**
	 * 發送 SSL POST 請求(HTTPS),K-V形式
	 * 
	 * @param apiUrl
	 *            API接口URL
	 * @param params
	 *            參數map
	 * @return
	 */
	public static String doPostSSL(String apiUrl, Map<String, Object> params){          
		//會發生unable to find valid certification path to requested target異常
		//CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(createSSLConnSocketFactory())
		//		.setConnectionManager(connMgr).setDefaultRequestConfig(requestConfig).build();
		HttpPost httpPost = new HttpPost(apiUrl);
		CloseableHttpResponse response = null;
		String httpStr = null;
		SSLClient httpClient = null;
		try {
			httpClient = new SSLClient();
			httpPost.setConfig(requestConfig);
			List<NameValuePair> pairList = new ArrayList<NameValuePair>(params.size());
			for (Map.Entry<String, Object> entry : params.entrySet()) {
				NameValuePair pair = new BasicNameValuePair(entry.getKey(), entry.getValue().toString());
				pairList.add(pair);
			}
			httpPost.setEntity(new UrlEncodedFormEntity(pairList, Charset.forName("utf-8")));
			response = httpClient.execute(httpPost);
			int statusCode = response.getStatusLine().getStatusCode();
			if (statusCode != HttpStatus.SC_OK) {
				return null;
			}
			HttpEntity entity = response.getEntity();
			if (entity == null) {
				return null;
			}
			httpStr = EntityUtils.toString(entity, "utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (response != null) {
				try {
					EntityUtils.consume(response.getEntity());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return httpStr;
	}

	/**
	 * 發送 SSL POST 請求(HTTPS),JSON形式
	 * 
	 * @param apiUrl
	 *            API接口URL
	 * @param json
	 *            JSON對象
	 * @return
	 */
	public static String doPostSSL(String apiUrl, Object json) {
		//CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(createSSLConnSocketFactory())
		//		.setConnectionManager(connMgr).setDefaultRequestConfig(requestConfig).build();
		HttpPost httpPost = new HttpPost(apiUrl);
		CloseableHttpResponse response = null;
		String httpStr = null;
 		SSLClient httpClient = null;
		try {
			httpClient = new SSLClient();
			httpPost.setConfig(requestConfig);
			StringEntity stringEntity = new StringEntity(json.toString(), "UTF-8");// 解決中文亂碼問題
			stringEntity.setContentEncoding("UTF-8");
			stringEntity.setContentType("application/json");
			httpPost.setEntity(stringEntity);
			response = httpClient.execute(httpPost);
			int statusCode = response.getStatusLine().getStatusCode();
			if (statusCode != HttpStatus.SC_OK) {
				return null;
			}
			HttpEntity entity = response.getEntity();
			if (entity == null) {
				return null;
			}
			httpStr = EntityUtils.toString(entity, "utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (response != null) {
				try {
					EntityUtils.consume(response.getEntity());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return httpStr;
	}

  

  以上方法都屬於HttpUtil類中。

SSL部分不涉及具體證書的使用。

還有很多不懂請見諒。只知道大概怎么用,大體的意思是知道的。

代碼片段:

https://github.com/617355557/code_pianduan


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM