package com.example.study;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import javax.net.ssl.*;
import java.io.*;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringBootStudyApplication.class)
@SuppressWarnings("all")
public class HttpClientTest {
//post 请求地址, 复杂的参数请求: 包含name, age , content 三个参数
private static final String url = "https://xxxxx:8443/https";
/**
* 经过https安全认证处理
*/
@Autowired
@Qualifier("httpsRestTemplate")
RestTemplate restTemplate;
@Test
public void sendHttpsRequest() {
HashMap<String, Object> hashMap = new HashMap<String, Object>();
hashMap.put("name", "java");
hashMap.put("age", 18);
hashMap.put("content", "{\"name\":\"java\",\"age\":\"20\"}");
MultiValueMap<String, Object> requestParams = new LinkedMultiValueMap<String, Object>();
requestParams.add("name", "java");
requestParams.add("age", 18);
Map<String,Object> temp = new HashMap<String,Object>();
temp.put("name", "java");
temp.put("age", 20);
requestParams.add("content", temp);
//HttpsURLConnection
String response = sendPost(url, hashMap);
System.out.println("HttpsURLConnection : " + response);
//HttpClient
String s = doPost(url, hashMap);
System.out.println("httpClient : " + s);
//restTemplate
String result = restTemplate.postForObject(url, requestParams, String.class);
System.out.println("restTemplate : " + result);
}
public static CloseableHttpClient createSSLClientDefault() {
try {
SSLContext sslcontext = SSLContext.getInstance("TLS");
sslcontext.init(null, new TrustManager[]{myX509TrustManager}, null);
HttpsURLConnection.setDefaultSSLSocketFactory(sslcontext.getSocketFactory());
// Create all-trusting host name verifier
HostnameVerifier allHostsValid = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
// Install the all-trusting host verifier
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, allHostsValid);
return HttpClients.custom().setSSLSocketFactory(sslsf).build();
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String doPost(String url, HashMap<String, Object> params) {
//构建POST请求 请求地址请更换为自己的。
HttpPost post = new HttpPost(url);
InputStream inputStream = null;
String result = "";
try {
//使用之前写的方法创建httpClient实例
//disableSslVerification();
CloseableHttpClient httpClient = createSSLClientDefault();
List<NameValuePair> paramsList = new ArrayList<>();
for (String key : params.keySet()) {
paramsList.add(new BasicNameValuePair(key, params.get(key).toString()));
}
System.out.println(paramsList);
post.setEntity(new UrlEncodedFormEntity(paramsList, "UTF-8"));
post.setHeader("Content-type", "application/x-www-form-urlencoded");
post.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
HttpResponse response = httpClient.execute(post);
org.apache.http.HttpEntity responseEntity = response.getEntity();
if (null != responseEntity && response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
inputStream = responseEntity.getContent();
byte[] buffer = new byte[1024];
int len;
StringBuffer sb = new StringBuffer();
while ((len = inputStream.read(buffer)) != -1) {
sb.append(new String(buffer, 0, len, "UTF-8"));
}
String content = sb.toString();
if (content.length() > 0) {
return content;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
private static void disableSslVerification() {
try {
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
}
};
// Install the all-trusting trust manager
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
// Create all-trusting host name verifier
HostnameVerifier allHostsValid = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
// Install the all-trusting host verifier
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
}
private static TrustManager myX509TrustManager = new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}
};
private static String sendPost(String url, Map<String, Object> params) {
disableSslVerification();
HttpsURLConnection connection = null;
List<NameValuePair> param = new ArrayList<>();
for (String key : params.keySet()) {
param.add(new BasicNameValuePair(key, params.get(key).toString()));
}
try {
String requestParams = URLEncodedUtils.format(param, "UTF-8");
URL postUrl = new URL(url + "?" + requestParams);
SSLContext sslcontext = SSLContext.getInstance("TLS");
sslcontext.init(null, new TrustManager[]{myX509TrustManager}, null);
connection = (HttpsURLConnection) postUrl.openConnection();
connection.setRequestProperty("Content-Type", "application/Json; charset=UTF-8");
connection.setConnectTimeout(30000);
connection.setReadTimeout(30000);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST"); // 默认是 GET方式
connection.setUseCaches(false); // Post 请求不能使用缓存
connection.setInstanceFollowRedirects(true); //设置本次连接是否自动重定向
connection.setRequestProperty("User-Agent", "Mozilla/5.0");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.addRequestProperty("Connection", "Keep-Alive");//设置与服务器保持连接
connection.setRequestProperty("Accept-Language", "zh-CN,zh;0.9");
connection.connect();
connection.setSSLSocketFactory(sslcontext.getSocketFactory());
int responseCode = connection.getResponseCode();
if (responseCode == 200) {
final BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
final StringBuffer stringBuffer = new StringBuffer(255);
synchronized (stringBuffer) {
while ((line = in.readLine()) != null) {
stringBuffer.append(line);
stringBuffer.append("\n");
}
System.out.println(stringBuffer.toString());
return stringBuffer.toString();
}
}
return null;
} catch (final IOException e) {
e.printStackTrace();
return null;
} catch (final Exception e1) {
e1.printStackTrace();
return null;
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
}