Httpclient


  今天給你們分享的是Httpclient,若有不足,敬請指教。java

1、Httpclient簡介

1.1 什麼是httpclient

  • HttpClient 是 Apache Jakarta Common 下的子項目,用來提供高效的、最新的、功能豐富的支持 HTTP 協議的客戶端編程工具包,而且它支持 HTTP 協議最新的版本和建議。實現了全部 HTTP 的方法(GET,POST,PUT,HEAD 等)
  • 下載地址:http://hc.apache.org/

1.2 httpclient做用

  • 在java代碼中,發送Http請求。一般用來實現遠程接口調用

1.3 HttpClient測試

  • 在工程中添加httpclient的pom依賴。
<!-- httpclient -->
	<dependency>
		<groupId>org.apache.httpcomponents</groupId>
		<artifactId>httpclient</artifactId>
		<version>4.3.5</version>
	</dependency>

1.3.1 執行GET請求

package com.xkt.test;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

/**
 * 測試HttpClient
 * 
 * @author lzx
 *
 */
public class HttpClientGet {

	public static void main(String[] args) {

		doGet();

	}

	private static void doGet() {

		CloseableHttpClient httpClient = null;

		CloseableHttpResponse response = null;

		try {
			// 1.建立客戶端
			httpClient = HttpClients.createDefault();

			// 2.建立get請求
			HttpGet get = new HttpGet("http://www.baidu.com");

			// 3.執行請求
			response = httpClient.execute(get);

			// 4.解析結果
			// 判斷返回狀態是否爲200
			int code = response.getStatusLine().getStatusCode();

			if (200 == code) {

				HttpEntity entity = response.getEntity();

				String str = EntityUtils.toString(entity, "utf-8");

				System.out.println(str);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != response) {
				try {
					response.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

			if (httpClient != null) {
				try {
					httpClient.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}

}

1.3.2 執行GET帶參數

package com.xkt.test;

import java.io.IOException;
import java.net.URI;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

/**
 * 測試HttpClient帶參數
 * 
 * @author lzx
 *
 */
public class HttpClientGetWithParam {

	public static void main(String[] args) {

		doGetWithParam();

	}

	private static void doGetWithParam() {

		CloseableHttpClient httpClient = null;

		CloseableHttpResponse response = null;

		try {
			// 1.建立客戶端
			httpClient = HttpClients.createDefault();

			// 2.建立get請求,當get請求有參數時,就在uri路徑上,須要用URIBuilder去構建參數

			URI uri = new URIBuilder("http://www.baidu.com/s").setParameter("wd", "java").build();
			HttpGet get = new HttpGet(uri);

			// 3.執行請求
			response = httpClient.execute(get);

			// 4.解析結果
			// 判斷返回狀態是否爲200
			int code = response.getStatusLine().getStatusCode();

			if (200 == code) {

				HttpEntity entity = response.getEntity();

				String str = EntityUtils.toString(entity, "utf-8");

				System.out.println(str);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != response) {
				try {
					response.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

			if (httpClient != null) {
				try {
					httpClient.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}

}

1.3.3 執行post請求

package com.xkt.test;

import java.io.IOException;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.LaxRedirectStrategy;
import org.apache.http.util.EntityUtils;

/**
 * 測試HttpClientPost
 * 
 * @author lzx
 *
 */
public class HttpClientPost {

	public static void main(String[] args) {

		doPost();

	}

	private static void doPost() {

		// 1.建立Httpclient對象
		CloseableHttpClient httpclient = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy())
				.build();

		// 2.建立http POST請求
		HttpPost httpPost = new HttpPost("http://www.oschina.net");

		CloseableHttpResponse response = null;

		/**
		 * HTTP/1.1 403 Forbidden 緣由: 有些網站,設置了反爬蟲機制 解決的辦法: 設置請求頭,假裝瀏覽器
		 */
		httpPost.addHeader("user-agent",
				"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");

		try {
			// 3.執行請求
			response = httpclient.execute(httpPost);
			System.out.println(response.getStatusLine());
			// 4.解析結果

			// 判斷返回狀態是否爲200
			if (response.getStatusLine().getStatusCode() == 200) {
				String content = EntityUtils.toString(response.getEntity(), "UTF-8");
				System.out.println(content);
			}
		} catch (Exception e) {
			e.printStackTrace();

		} finally {
			if (response != null) {
				try {
					response.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			try {
				httpclient.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}

1.3.4 執行post帶參數

package com.xkt.test;

import java.io.IOException;
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.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.LaxRedirectStrategy;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

/**
 * 測試HttpClientPostWithParam
 * 
 * @author lzx
 *
 */
public class HttpClientPostWithParam {

	public static void main(String[] args) {

		doPostWithParam();

	}

	private static void doPostWithParam() {

		CloseableHttpClient httpclient = null;
		CloseableHttpResponse response = null;

		try {
			// 一、第一步:建立HttpClient客戶端
			httpclient = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build();

			// 二、第二步:建立post請求方式,若是有參數,須要經過UrlEncodedFormEntity來模擬form表單

			// 設置2個post參數,一個是scope、一個是q
			List<NameValuePair> parameters = new ArrayList<>();
			parameters.add(new BasicNameValuePair("scope", "project"));
			parameters.add(new BasicNameValuePair("q", "mysql"));
			// 構造一個form表單式的實體
			UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters);
			HttpPost post = new HttpPost("http://www.oschina.net/search");
			// 將請求實體設置到httpPost對象中
			post.setEntity(formEntity);

			// 有一些網站(接口)設置了反爬蟲機制,禁止httpclient這種工具去請求接口代碼
			// 設置請求頭,假裝瀏覽器發送請求,有的網站機制比較健全,可能會不成功
			post.addHeader("user-agent",
					"Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36");

			// 三、第三步:執行請求

			response = httpclient.execute(post);

			// 四、第四步:解析結果
			int statusCode = response.getStatusLine().getStatusCode();
			System.out.println(statusCode);
			if (200 == statusCode) {
				HttpEntity entity = response.getEntity();

				String str = EntityUtils.toString(entity, "utf-8");

				System.out.println(str);
			}

		} catch (Exception e) {

			e.printStackTrace();
		} finally {
			if (null != response) {
				try {
					response.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

			if (null != httpclient) {
				try {
					httpclient.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}
}

1.3.5 封裝通用工具類HttpClientUtils

package com.xkt.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

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.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.client.LaxRedirectStrategy;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

/**
 * 
 * 封裝HttpClient工具類
 * 
 * @author Administrator
 *
 */
public class HttpClientUtils {

	public static String doGet(String url, Map<String, String> params) {

		// 獲取httpclient客戶端
		CloseableHttpClient httpclient = HttpClients.createDefault();

		String resultString = "";

		CloseableHttpResponse response = null;

		try {
			URIBuilder builder = new URIBuilder(url);

			if (null != params) {
				for (String key : params.keySet()) {
					builder.setParameter(key, params.get(key));
				}
			}

			HttpGet get = new HttpGet(builder.build());

			response = httpclient.execute(get);

			System.out.println(response.getStatusLine());

			if (200 == response.getStatusLine().getStatusCode()) {
				HttpEntity entity = response.getEntity();
				resultString = EntityUtils.toString(entity, "utf-8");
			}

		} catch (Exception e) {

			e.printStackTrace();
		} finally {
			if (null != response) {
				try {
					response.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (null != httpclient) {
				try {
					httpclient.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

		return resultString;
	}

	public static String doGet(String url) {
		return doGet(url, null);
	}

	public static String doPost(String url, Map<String, String> params) {
		/**
		 * 在4.0及以上httpclient版本中,post須要指定重定向的策略,若是不指定則按默認的重定向策略。
		 * 
		 * 獲取httpclient客戶端
		 */
		CloseableHttpClient httpclient = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy())
				.build();

		String resultString = "";

		CloseableHttpResponse response = null;

		try {

			HttpPost post = new HttpPost(url);

			List<NameValuePair> paramaters = new ArrayList<>();

			if (null != params) {
				for (String key : params.keySet()) {
					paramaters.add(new BasicNameValuePair(key, params.get(key)));
				}

				UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(paramaters);

				post.setEntity(formEntity);
			}

			/**
			 * HTTP/1.1 403 Forbidden 緣由: 有些網站,設置了反爬蟲機制 解決的辦法: 設置請求頭,假裝瀏覽器
			 */
			post.addHeader("user-agent",
					"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");

			response = httpclient.execute(post);

			System.out.println(response.getStatusLine());

			if (200 == response.getStatusLine().getStatusCode()) {
				HttpEntity entity = response.getEntity();
				resultString = EntityUtils.toString(entity, "utf-8");
			}

		} catch (Exception e) {

			e.printStackTrace();
		} finally {
			if (null != response) {
				try {
					response.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (null != httpclient) {
				try {
					httpclient.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

		return resultString;
	}

	public static String doPost(String url) {
		return doPost(url, null);
	}
}

1.4 httpclient常見問題及解決方案

1.4.1 請求參數亂碼

  • 設置請求的編碼格式:
obj.addHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");

1.4.2 響應HTTP/1.1 403 Forbidden

  • 緣由:網站設置了反爬蟲機制,禁止非法訪問。
  • 解決方案:假裝瀏覽器。
obj.addHeader("User-Agent"," Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537. 36 (KHTML, like Gecko) Chrome/31.0.1650.63");

版權說明:歡迎以任何方式進行轉載,但請在轉載後註明出處!mysql

相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息