HttpClient工具類

HttpClient工具類

1. 什麼是HttpClient

HTTP 協議多是如今 Internet 上使用得最多、最重要的協議了,愈來愈多的 Java 應用程序須要直接經過 HTTP 協議來訪問網絡資源。雖然在 JDK 的 java net包中已經提供了訪問 HTTP 協議的基本功能,可是對於大部分應用程序來講,JDK 庫自己提供的功能還不夠豐富和靈活。HttpClient 是Apache HttpComponents 下的子項目,用來提供高效的、最新的、功能豐富的支持 HTTP 協議的客戶端編程工具包,而且它支持 HTTP 協議最新的版本和建議。javascript

2. 功能介紹

  • 實現了全部 HTTP 的方法(GET,POST,PUT,DELETE 等)
  • 支持自動轉向
  • 支持 HTTPS 協議
  • 支持代理服務器等

3. 版本比較

主要是基於 HttpClient4.5.5 版本的來說解的,也是如今最新的版本,之因此要提供版本說明的是由於 HttpClient 3 版本和 HttpClient 4 版本差異仍是不少大的,基本HttpClient裏面的接口都變了,你把 HttpClient 3 版本的代碼拿到 HttpClient 4 上面都運行不起來,會報錯的。因此必定要注意 HtppClient 的版本問題。html

4. HttpClient不能作的事情

HttpClient 不是瀏覽器,它是一個客戶端 HTTP 協議傳輸類庫。HttpClient 被用來發送和接受 HTTP 消息。HttpClient 不會處理 HTTP 消息的內容,不會進行 javascript 解析,不會關心 content type,若是沒有明確設置,HttpClient 也不會對請求進行格式化、重定向 url,或者其餘任何和 HTTP 消息傳輸相關的功能。java

5. HttpClient使用流程

使用HttpClient發送請求、接收響應很簡單,通常須要以下幾步便可。git

  1. 建立HttpClient對象。
  2. 建立請求方法的實例,並指定請求URL。若是須要發送GET請求,建立HttpGet對象;若是須要發送POST請求,建立HttpPost對象。
  3. 若是須要發送請求參數,可調用HttpGetsetParams方法來添加請求參數;對於HttpPost對象而言,可調用setEntity(HttpEntity entity)方法來設置請求參數。
  4. 調用HttpClient對象的execute(HttpUriRequest request)發送請求,該方法返回一個HttpResponse對象。
  5. 調用HttpResponse的getAllHeaders()、getHeaders(String name)等方法可獲取服務器的響應頭;調用HttpResponse的getEntity()方法可獲取HttpEntity對象,該對象包裝了服務器的響應內容。程序可經過該對象獲取服務器的響應內容。
  6. 釋放鏈接。不管執行方法是否成功,都必須釋放鏈接
<properties>
        <httpclient.version>4.5.5</httpclient.version>

        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <dependencies>
        <!-- springboot的web和test啓動庫 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

        <!-- apache httpclient組件 -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </dependency>

    </dependencies>

    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <!-- 跳過單元測試 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
        </plugins>
    </build>
複製代碼

6.2 編寫spring-boot啓動類

/**
 * Description: springboot啓動類
 * 
 * @author JourWon
 * @date Created on 2018年4月19日
 */
@SpringBootApplication
public class Application {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

}
複製代碼

6.3 編寫get和post請求測試controller

/**
 * Description: get和post請求測試controller
 * 
 * @author JourWon
 * @date Created on 2018年4月19日
 */
@RestController
@RequestMapping("/hello")
public class HelloWorldController {

	@GetMapping("/get")
	public String get() throws InterruptedException {
		return "get無參請求成功";
	}

	@GetMapping("/getWithParam")
	public String getWithParam(@RequestParam String message) {
		return "get帶參請求成功,參數message: " + message;
	}

	@PostMapping("/post")
	public String post(@RequestHeader("User-Agent") String userAgent,
            @RequestHeader("Accept") String accept,
            @RequestHeader("Accept-Language") String acceptLanguage,
            @RequestHeader("Accept-Encoding") String acceptEncoding,
            @RequestHeader("Cookie") String cookie,
            @RequestHeader("Connection") String conn) {
		// 打印請求頭信息
		System.out.println("Cookie = " + cookie);
		System.out.println("Connection = " + conn);
		System.out.println("Accept = " + accept);
		System.out.println("Accept-Language = " + acceptLanguage);
		System.out.println("Accept-Encoding = " + acceptEncoding);
		System.out.println("User-Agent = " + userAgent);
		
		return "post無參請求成功";
	}

	@PostMapping("/postWithParam")
	public String postWithParam(@RequestParam String code, @RequestParam String message) {
		return "post帶參請求成功,參數code: " + code + ",參數message: " + message;
	}

}
複製代碼

6.4 建立httpClient響應結果對象

/**
 * Description: 封裝httpClient響應結果
 * 
 * @author JourWon
 * @date Created on 2018年4月19日
 */
public class HttpClientResult implements Serializable {

	/**
	 * 響應狀態碼
	 */
	private int code;

	/**
	 * 響應數據
	 */
	private String content;

}
複製代碼

6.5 重點,編寫httpclient工具類

/**
 * Description: httpClient工具類
 * 
 * @author JourWon
 * @date Created on 2018年4月19日
 */
public class HttpClientUtils {

	// 編碼格式。發送編碼格式統一用UTF-8
	private static final String ENCODING = "UTF-8";
	
	// 設置鏈接超時時間,單位毫秒。
	private static final int CONNECT_TIMEOUT = 6000;
	
	// 請求獲取數據的超時時間(即響應時間),單位毫秒。
	private static final int SOCKET_TIMEOUT = 6000;

	/**
	 * 發送get請求;不帶請求頭和請求參數
	 * 
	 * @param url 請求地址
	 * @return
	 * @throws Exception
	 */
	public static HttpClientResult doGet(String url) throws Exception {
		return doGet(url, null, null);
	}
	
	/**
	 * 發送get請求;帶請求參數
	 * 
	 * @param url 請求地址
	 * @param params 請求參數集合
	 * @return
	 * @throws Exception
	 */
	public static HttpClientResult doGet(String url, Map<String, String> params) throws Exception {
		return doGet(url, null, params);
	}

	/**
	 * 發送get請求;帶請求頭和請求參數
	 * 
	 * @param url 請求地址
	 * @param headers 請求頭集合
	 * @param params 請求參數集合
	 * @return
	 * @throws Exception
	 */
	public static HttpClientResult doGet(String url, Map<String, String> headers, Map<String, String> params) throws Exception {
		// 建立httpClient對象
		CloseableHttpClient httpClient = HttpClients.createDefault();

		// 建立訪問的地址
		URIBuilder uriBuilder = new URIBuilder(url);
		if (params != null) {
			Set<Entry<String, String>> entrySet = params.entrySet();
			for (Entry<String, String> entry : entrySet) {
				uriBuilder.setParameter(entry.getKey(), entry.getValue());
			}
		}

		// 建立http對象
		HttpGet httpGet = new HttpGet(uriBuilder.build());
		/**
		 * setConnectTimeout:設置鏈接超時時間,單位毫秒。
		 * setConnectionRequestTimeout:設置從connect Manager(鏈接池)獲取Connection
		 * 超時時間,單位毫秒。這個屬性是新加的屬性,由於目前版本是能夠共享鏈接池的。
		 * setSocketTimeout:請求獲取數據的超時時間(即響應時間),單位毫秒。 若是訪問一個接口,多少時間內沒法返回數據,就直接放棄這次調用。
		 */
		RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build();
		httpGet.setConfig(requestConfig);
		
		// 設置請求頭
		packageHeader(headers, httpGet);

		// 建立httpResponse對象
		CloseableHttpResponse httpResponse = null;

		try {
			// 執行請求並得到響應結果
			return getHttpClientResult(httpResponse, httpClient, httpGet);
		} finally {
			// 釋放資源
			release(httpResponse, httpClient);
		}
	}

	/**
	 * 發送post請求;不帶請求頭和請求參數
	 * 
	 * @param url 請求地址
	 * @return
	 * @throws Exception
	 */
	public static HttpClientResult doPost(String url) throws Exception {
		return doPost(url, null, null);
	}
	
	/**
	 * 發送post請求;帶請求參數
	 * 
	 * @param url 請求地址
	 * @param params 參數集合
	 * @return
	 * @throws Exception
	 */
	public static HttpClientResult doPost(String url, Map<String, String> params) throws Exception {
		return doPost(url, null, params);
	}

	/**
	 * 發送post請求;帶請求頭和請求參數
	 * 
	 * @param url 請求地址
	 * @param headers 請求頭集合
	 * @param params 請求參數集合
	 * @return
	 * @throws Exception
	 */
	public static HttpClientResult doPost(String url, Map<String, String> headers, Map<String, String> params) throws Exception {
		// 建立httpClient對象
		CloseableHttpClient httpClient = HttpClients.createDefault();

		// 建立http對象
		HttpPost httpPost = new HttpPost(url);
		/**
		 * setConnectTimeout:設置鏈接超時時間,單位毫秒。
		 * setConnectionRequestTimeout:設置從connect Manager(鏈接池)獲取Connection
		 * 超時時間,單位毫秒。這個屬性是新加的屬性,由於目前版本是能夠共享鏈接池的。
		 * setSocketTimeout:請求獲取數據的超時時間(即響應時間),單位毫秒。 若是訪問一個接口,多少時間內沒法返回數據,就直接放棄這次調用。
		 */
		RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build();
		httpPost.setConfig(requestConfig);
		// 設置請求頭
		/*httpPost.setHeader("Cookie", "");
		httpPost.setHeader("Connection", "keep-alive");
		httpPost.setHeader("Accept", "application/json");
		httpPost.setHeader("Accept-Language", "zh-CN,zh;q=0.9");
		httpPost.setHeader("Accept-Encoding", "gzip, deflate, br");
		httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");*/
		packageHeader(headers, httpPost);
		
		// 封裝請求參數
		packageParam(params, httpPost);

		// 建立httpResponse對象
		CloseableHttpResponse httpResponse = null;

		try {
			// 執行請求並得到響應結果
			return getHttpClientResult(httpResponse, httpClient, httpPost);
		} finally {
			// 釋放資源
			release(httpResponse, httpClient);
		}
	}

	/**
	 * 發送put請求;不帶請求參數
	 * 
	 * @param url 請求地址
	 * @param params 參數集合
	 * @return
	 * @throws Exception
	 */
	public static HttpClientResult doPut(String url) throws Exception {
		return doPut(url);
	}

	/**
	 * 發送put請求;帶請求參數
	 * 
	 * @param url 請求地址
	 * @param params 參數集合
	 * @return
	 * @throws Exception
	 */
	public static HttpClientResult doPut(String url, Map<String, String> params) throws Exception {
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpPut httpPut = new HttpPut(url);
		RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build();
		httpPut.setConfig(requestConfig);
		
		packageParam(params, httpPut);

		CloseableHttpResponse httpResponse = null;

		try {
			return getHttpClientResult(httpResponse, httpClient, httpPut);
		} finally {
			release(httpResponse, httpClient);
		}
	}

	/**
	 * 發送delete請求;不帶請求參數
	 * 
	 * @param url 請求地址
	 * @param params 參數集合
	 * @return
	 * @throws Exception
	 */
	public static HttpClientResult doDelete(String url) throws Exception {
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpDelete httpDelete = new HttpDelete(url);
		RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build();
		httpDelete.setConfig(requestConfig);

		CloseableHttpResponse httpResponse = null;
		try {
			return getHttpClientResult(httpResponse, httpClient, httpDelete);
		} finally {
			release(httpResponse, httpClient);
		}
	}

	/**
	 * 發送delete請求;帶請求參數
	 * 
	 * @param url 請求地址
	 * @param params 參數集合
	 * @return
	 * @throws Exception
	 */
	public static HttpClientResult doDelete(String url, Map<String, String> params) throws Exception {
		if (params == null) {
			params = new HashMap<String, String>();
		}

		params.put("_method", "delete");
		return doPost(url, params);
	}
	
	/**
	 * Description: 封裝請求頭
	 * @param params
	 * @param httpMethod
	 */
	public static void packageHeader(Map<String, String> params, HttpRequestBase httpMethod) {
		// 封裝請求頭
		if (params != null) {
			Set<Entry<String, String>> entrySet = params.entrySet();
			for (Entry<String, String> entry : entrySet) {
				// 設置到請求頭到HttpRequestBase對象中
				httpMethod.setHeader(entry.getKey(), entry.getValue());
			}
		}
	}

	/**
	 * Description: 封裝請求參數
	 * 
	 * @param params
	 * @param httpMethod
	 * @throws UnsupportedEncodingException
	 */
	public static void packageParam(Map<String, String> params, HttpEntityEnclosingRequestBase httpMethod)
			throws UnsupportedEncodingException {
		// 封裝請求參數
		if (params != null) {
			List<NameValuePair> nvps = new ArrayList<NameValuePair>();
			Set<Entry<String, String>> entrySet = params.entrySet();
			for (Entry<String, String> entry : entrySet) {
				nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
			}

			// 設置到請求的http對象中
			httpMethod.setEntity(new UrlEncodedFormEntity(nvps, ENCODING));
		}
	}

	/**
	 * Description: 得到響應結果
	 * 
	 * @param httpResponse
	 * @param httpClient
	 * @param httpMethod
	 * @return
	 * @throws Exception
	 */
	public static HttpClientResult getHttpClientResult(CloseableHttpResponse httpResponse,
			CloseableHttpClient httpClient, HttpRequestBase httpMethod) throws Exception {
		// 執行請求
		httpResponse = httpClient.execute(httpMethod);

		// 獲取返回結果
		if (httpResponse != null && httpResponse.getStatusLine() != null) {
			String content = "";
			if (httpResponse.getEntity() != null) {
				content = EntityUtils.toString(httpResponse.getEntity(), ENCODING);
			}
			return new HttpClientResult(httpResponse.getStatusLine().getStatusCode(), content);
		}
		return new HttpClientResult(HttpStatus.SC_INTERNAL_SERVER_ERROR);
	}

	/**
	 * Description: 釋放資源
	 * 
	 * @param httpResponse
	 * @param httpClient
	 * @throws IOException
	 */
	public static void release(CloseableHttpResponse httpResponse, CloseableHttpClient httpClient) throws IOException {
		// 釋放資源
		if (httpResponse != null) {
			httpResponse.close();
		}
		if (httpClient != null) {
			httpClient.close();
		}
	}

}
複製代碼

6.6 啓動spring-boot,測試get、post請求

/** * Description: HttpClientUtils工具類測試 * * @author JourWon * @date Created on 2018年4月19日 */
public class HttpClientUtilsTest {

	/** * Description: 測試get無參請求 * * @throws Exception */
	@Test
	public void testGet() throws Exception {
		HttpClientResult result = HttpClientUtils.doGet("http://127.0.0.1:8080/hello/get");
		System.out.println(result);
	}

	/** * Description: 測試get帶參請求 * * @throws Exception */
	@Test
	public void testGetWithParam() throws Exception {
		Map<String, String> params = new HashMap<String, String>();
		params.put("message", "helloworld");
		HttpClientResult result = HttpClientUtils.doGet("http://127.0.0.1:8080/hello/getWithParam", params);
		System.out.println(result);
	}

	/** * Description: 測試post帶請求頭不帶請求參數 * * @throws Exception */
	@Test
	public void testPost() throws Exception {
		Map<String, String> headers = new HashMap<String, String>();
		headers.put("Cookie", "123");
		headers.put("Connection", "keep-alive");
		headers.put("Accept", "application/json");
		headers.put("Accept-Language", "zh-CN,zh;q=0.9");
		headers.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");
		HttpClientResult result = HttpClientUtils.doPost("http://127.0.0.1:8080/hello/post", headers, null);
		System.out.println(result);
	}

	/** * Description: 測試post帶參請求 * * @throws Exception */
	@Test
	public void testPostWithParam() throws Exception {
		Map<String, String> params = new HashMap<String, String>();
		params.put("code", "0");
		params.put("message", "helloworld");
		HttpClientResult result = HttpClientUtils.doPost("http://127.0.0.1:8080/hello/postWithParam", params);
		System.out.println(result);
	}

}
複製代碼

HttpsUtils工具類

實現利用HttpClient發送Https請求,信任任何證書、不對主機校驗github

參見官方文檔web

1571896539517

public class HttpsUtils {
    private static final String HTTP = "http";
    private static final String HTTPS = "https";
    private static SSLConnectionSocketFactory sslsf = null;
    private static PoolingHttpClientConnectionManager cm = null;
    private static SSLContextBuilder builder = null;
    static {
        try {
            builder = new SSLContextBuilder();
            // 所有信任 不作身份鑑定
            builder.loadTrustMaterial(null, (org.apache.http.ssl.TrustStrategy) (x509Certificates, s) -> true);
            sslsf = new SSLConnectionSocketFactory(builder.build(), new String[]{"SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.2"},
                    null, NoopHostnameVerifier.INSTANCE);
            //設置協議http和https對應的處理socket鏈接工廠的對象
            Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
                    .register(HTTP, new PlainConnectionSocketFactory())
                    .register(HTTPS, sslsf)
                    .build();
            cm = new PoolingHttpClientConnectionManager(registry);
            cm.setMaxTotal(200);//max connection
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /** * httpClient get請求 * @param url 請求url * @param * @param * @return 可能爲空 須要處理 * @throws Exception * */
    public static String get(String url) throws Exception {
        String result = "";
        CloseableHttpClient httpClient = null;
        try {
            httpClient = getHttpClient();
            HttpGet httpGet = new HttpGet(url);
            // 設置頭信息 ,必須添加,不然會報403
            httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36");

            HttpResponse httpResponse = httpClient.execute(httpGet);
            int statusCode = httpResponse.getStatusLine().getStatusCode();
            if (statusCode == HttpStatus.SC_OK) {
                HttpEntity resEntity = httpResponse.getEntity();
                result = EntityUtils.toString(resEntity, "utf-8");
            } else {
            	readHttpResponse(httpResponse);
            }
        } catch (Exception e) {throw e;
        } finally {
            if (httpClient != null) {
                httpClient.close();
            }
        }
        return result;
    }
    public static CloseableHttpClient getHttpClient() throws Exception {
        CloseableHttpClient httpClient = HttpClients.custom()
                .setSSLSocketFactory(sslsf)
                .setConnectionManager(cm)
                .setConnectionManagerShared(true)
                .build();
        return httpClient;
    }
    public static String readHttpResponse(HttpResponse httpResponse) throws ParseException, IOException {
        StringBuilder builder = new StringBuilder();
        // 獲取響應消息實體
        HttpEntity entity = httpResponse.getEntity();
        // 響應狀態
        builder.append("status:" + httpResponse.getStatusLine());
        builder.append("headers:");
        HeaderIterator iterator = httpResponse.headerIterator();
        while (iterator.hasNext()) {
            builder.append("\t" + iterator.next());
        }
        // 判斷響應實體是否爲空
        if (entity != null) {
            String responseString = EntityUtils.toString(entity);
            builder.append("response length:" + responseString.length());
            builder.append("response content:" + responseString.replace("\r\n", ""));
        }
        return builder.toString();
    }
}
複製代碼

參考github開源工具spring

github.com/Arronlong/h…apache

github.com/JourWon/htt…編程

相關文章
相關標籤/搜索