import org.springframework.http.HttpStatus;
import javax.net.ssl.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
/**
* 通过HttpUrlConnect调用Https asmx soap接口(绕过证书)
*
* @Author lby
* @Create 2019-11-3 11:41
*/
public class HttpsUrlConnectUtil {
public static void main(String args[]) throws IOException {
String xmlRequestBody =
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
+ " <soap:Body>\n"
+ " <GetUserStatus xmlns=\"https://xxxx.com/WebService\">\n"
+ " <systemIdentificationCode>xxxxx</systemIdentificationCode>\n"
+ " <functionAuthorizationCode>xxxxx</functionAuthorizationCode>\n"
+ " <jsonOrganizationStatus></jsonOrganizationStatus>\n"
+ " </GetUserStatus>\n"
+ " </soap:Body>\n"
+ "</soap:Envelope>";
// 服务器地址
String serverUrl = "https://xxxx.com/WebService/xxxxService.asmx";
// 需要调用的方法
String soapAction = "GetUserStatus";
conn(xmlRequestBody, serverUrl, soapAction);
}
/**
* 调用Https asmx SOAP接口
*
* @param xmlRequestBody SOAP XML请求体
* @param serverUrl SOAP服务器地址
* @param soapAction
* @throws IOException
*/
public static void conn(String xmlRequestBody, String serverUrl, String soapAction) throws IOException {
URL url = new URL(serverUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
HttpsURLConnection https = (HttpsURLConnection) conn;
trustAllHosts(https);
https.setHostnameVerifier(DO_NOT_VERIFY);
// 拼接soap
String soap = xmlRequestBody;
byte[] buf = soap.getBytes("UTF-8");
// 设置报头
conn.setRequestProperty("Content-Length", String.valueOf(buf.length));
conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
conn.setRequestProperty("soapActionString", soapAction);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
OutputStream out = conn.getOutputStream();
out.write(buf);
out.close();
// 获取响应状态码
int code = conn.getResponseCode();
StringBuffer sb = new StringBuffer();
if (code == HttpStatus.OK.value()) {
InputStream is = conn.getInputStream();
byte[] b = new byte[1024];
int len = 0;
while ((len = is.read(b)) != -1) {
String s = new String(b, 0, len, "utf-8");
sb.append(s);
}
is.close();
}
System.out.println(sb);
}
/**
* 覆盖java默认的证书验证
*/
private static final TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[]{};
}
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}
}};
/**
* 设置不验证主机
*/
private static final HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
/**
* 信任所有
*
* @param connection
* @return
*/
private static SSLSocketFactory trustAllHosts(HttpsURLConnection connection) {
SSLSocketFactory oldFactory = connection.getSSLSocketFactory();
try {
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
SSLSocketFactory newFactory = sc.getSocketFactory();
connection.setSSLSocketFactory(newFactory);
} catch (Exception e) {
e.printStackTrace();
}
return oldFactory;
}
}