最近项目中需要向外部服务发送https请求,但是外部服务测试环境提供的访问地址为https加ip、端口的访问方式,由于SSL签名认证问题(网上搜索大多说是因为https后面设计为跟域名绑定访问),无法访问,后来尝试网上各大神的解决方案,完美解决该问题。将代码保存下来,以备不时之需。
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
/**
* @param :
* @author : mym
* @version : V1.0
* @date : 2019/07/16 17:25
* @describe : 发送http请求
* @return :
*/
public class HttpClientUtil {
/* *
* @author : mym
* @date Date : 2019/7/20 11:16
* @version : V1.0
* @describe : https发送json格式的请求 出参、入参都是json串
* @param :
* @return :
*/
public static String doJsonPost(String url,String jsonstr,String charset){
HttpClient httpClient = null;
HttpPost httpPost = null;
String result = null;
try{
httpClient = new SSLClient();
httpPost = new HttpPost(url);
httpPost.addHeader("Content-Type", "application/json");
StringEntity se = new StringEntity(jsonstr,charset);
se.setContentType("text/json");
se.setContentEncoding(new BasicHeader("Content-Type", "application/json"));
httpPost.setEntity(se);
HttpResponse response = httpClient.execute(httpPost);
if(response != null){
HttpEntity resEntity = response.getEntity();
if(resEntity != null){
result = EntityUtils.toString(resEntity,charset);
}
}
}catch(Exception e){
e.printStackTrace();
}
return result;
}
/* *
* @author : mym
* @date Date : 2019/7/20 11:17
* @version : V1.0
* @describe : http发送xml格式的请求 出参、入参都是xml
* @param :
* @return :
*/
public static String doXmlPost(String url,String xml,String charset){
HttpClient httpClient = null;
HttpPost httpPost = null;
String result = null;
try{
httpClient = new SSLClient();
httpPost = new HttpPost(url);
httpPost.addHeader("Content-Type", "application/xml");
StringEntity se = new StringEntity(xml,charset);
se.setContentType("text/xml");
se.setContentEncoding(new BasicHeader("Content-Type", "application/xml"));
httpPost.setEntity(se);
HttpResponse response = httpClient.execute(httpPost);
if(response != null){
HttpEntity resEntity = response.getEntity();
if(resEntity != null){
result = EntityUtils.toString(resEntity,charset);
}
}
}catch(Exception e){
e.printStackTrace();
}
return result;
}
}
import org.apache.http.conn.ClientConnectionManager;
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;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
/**
* @param :
* @author : mym
* @version : V1.0
* @date : 2019/07/16 17:21
* @describe : 信任https 签名
* @return :
*/
public class SSLClient extends DefaultHttpClient {
public SSLClient() throws Exception{
super();
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
ctx.init(null, new TrustManager[]{tm}, null);
SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
ClientConnectionManager ccm = this.getConnectionManager();
SchemeRegistry sr = ccm.getSchemeRegistry();
sr.register(new Scheme("https", 443, ssf));
}
}
需要的maven依赖:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.5</version>
</dependency>
码字不易,转载请注明出处!