通过HttpUrlConnect调用Https asmx soap接口(绕过证书)

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;
    }
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值