SpringCloud—config分散式配置
阿新 • • 發佈:2021-01-31
技術標籤:Java
一、概念
HttpClient是Apache Jakarta Common下的子專案,用來提供高效的、最新的、功能豐富的支援HTTP協議的客戶端程式設計工具包,並且它支援HTTP協議最新的版本和建議。 HttpClient已經應用在很多的專案中,比如Apache Jakarta上很著名的另外兩個開源專案Cactus和HTMLUnit都使用了HttpClient。 HttpClient通俗的講就是模擬了瀏覽器的行為,如果我們需要在後端向某一地址提交資料獲取結果,就可以使用HttpClient。 關於HttpClient(原生)具體的使用不屬於我們本章的學習內容,我們這裡這裡為了簡化HttpClient的使用,提供了工具類HttpClient(對原生HttpClient進行了封裝)。
二、HttpClient工具類程式碼
public class HttpClient {
private String url;
private Map<String, String> param;
private int statusCode;
private String content;
private String xmlParam;
private boolean isHttps;
public boolean isHttps() {
return isHttps;
}
public void setHttps(boolean isHttps) {
this.isHttps = isHttps;
}
public String getXmlParam() {
return xmlParam;
}
public void setXmlParam(String xmlParam) {
this.xmlParam = xmlParam;
}
public HttpClient(String url, Map<String, String> param) {
this .url = url;
this.param = param;
}
public HttpClient(String url) {
this.url = url;
}
public void setParameter(Map<String, String> map) {
param = map;
}
public void addParameter(String key, String value) {
if (param == null)
param = new HashMap<String, String>();
param.put(key, value);
}
public void post() throws ClientProtocolException, IOException {
HttpPost http = new HttpPost(url);
setEntity(http);
execute(http);
}
public void put() throws ClientProtocolException, IOException {
HttpPut http = new HttpPut(url);
setEntity(http);
execute(http);
}
public void get() throws ClientProtocolException, IOException {
if (param != null) {
StringBuilder url = new StringBuilder(this.url);
boolean isFirst = true;
for (String key : param.keySet()) {
if (isFirst) {
url.append("?");
}else {
url.append("&");
}
url.append(key).append("=").append(param.get(key));
}
this.url = url.toString();
}
HttpGet http = new HttpGet(url);
execute(http);
}
/**
* set http post,put param
*/
private void setEntity(HttpEntityEnclosingRequestBase http) {
if (param != null) {
List<NameValuePair> nvps = new LinkedList<NameValuePair>();
for (String key : param.keySet()) {
nvps.add(new BasicNameValuePair(key, param.get(key))); // 引數
}
http.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8)); // 設定引數
}
if (xmlParam != null) {
http.setEntity(new StringEntity(xmlParam, Consts.UTF_8));
}
}
private void execute(HttpUriRequest http) throws ClientProtocolException,
IOException {
CloseableHttpClient httpClient = null;
try {
if (isHttps) {
SSLContext sslContext = new SSLContextBuilder()
.loadTrustMaterial(null, new TrustStrategy() {
// 信任所有
@Override
public boolean isTrusted(X509Certificate[] chain,
String authType)
throws CertificateException {
return true;
}
}).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslContext);
httpClient = HttpClients.custom().setSSLSocketFactory(sslsf)
.build();
} else {
httpClient = HttpClients.createDefault();
}
CloseableHttpResponse response = httpClient.execute(http);
try {
if (response != null) {
if (response.getStatusLine() != null) {
statusCode = response.getStatusLine().getStatusCode();
}
HttpEntity entity = response.getEntity();
// 響應內容
content = EntityUtils.toString(entity, Consts.UTF_8);
}
} finally {
response.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
httpClient.close();
}
}
public int getStatusCode() {
return statusCode;
}
public String getContent() throws ParseException, IOException {
return content;
}
}
三、HttpClient工具類使用的步驟
- 匯入依賴
<!--httpclient支援-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
- 測試案例
/**
* @Author TeaBowl
* @Date 2021/2/1 4:50
* @Version 1.0
* HttpClient工具類的使用
* 作用:傳送Http、Https請求
*/
public class HttpClientTest {
/**
* 傳送Http、Https請求
* 傳送指定引數
* 可以獲取相應的結果
*/
@Test
public void testHttpClient() throws IOException {
//請求地址
String url = "https://www.cnblogs.com/chawaner/";
//建立HttpClient物件,對請求地址進行操作
HttpClient httpClient = new HttpClient(url);
//建立要傳送的xml資料->post
String xml = "<xml><name>茶碗兒</name></xml>";
//使用HttpClient物件,設定請求的XML引數
httpClient.setXmlParam(xml);
//傳送請求
//設定當前請求為Https
httpClient.setHttps(true);
//傳送XML資料,使用post請求
httpClient.post();
//獲取響應資料
String content = httpClient.getContent();
System.out.println(content);
}
}