https请求SOAP webService接口

简介: https请求SOAP webService接口

本文章向大家介绍 https访问带有SOAP协议头,需要用户验证的webservice接口,主要包括https请求SOAP webService接口使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。


模拟soapui调用webservice

public static String soapSpecialConnection(String url,String params) throws Exception {
        String s = new String();
        StringBuilder soapHeader = new StringBuilder();
        // 传来字符串参数
        soapHeader.append(params);
        System.out.println("soapHeader=" + soapHeader);
        // 设置soap请求报文的相关属性
        // url从soapUI的request1的RAW标签的POST获取,url中不要有空格
        URL u = new URL(url);
        HttpURLConnection conn = (HttpURLConnection) u.openConnection();
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        conn.setDefaultUseCaches(false);
        // Host,Content-Type,SOAPAction从soapUI的request1的RAW标签的Host,Content-Typ,SOAPActione获取
        conn.setRequestProperty("Host", "test.com:95443");
        conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
        conn.setRequestProperty("Accept-Encoding", "gzip,deflate");
        conn.setRequestProperty("Authorization", "Basic ABCBCBC");
        conn.setRequestProperty("Content-Length", String.valueOf(soapHeader.length()));
        conn.setRequestProperty("SOAPAction", "");
        conn.setRequestProperty("Username", "AAAAA");
        conn.setRequestProperty("Password", "BBBB");
        // 定义输出流
        OutputStream output = conn.getOutputStream();
        if (null != soapHeader) {
            byte[] b = soapHeader.toString().getBytes("utf-8");
            // 发送soap请求报文
            output.write(b, 0, b.length);
        }
        output.flush();
        output.close();
        // 定义输入流,获取soap响应报文
        InputStream input = conn.getInputStream();
        // 需设置编码格式,否则会乱码
        s = IOUtils.toString(input, "UTF-8");
        input.close();
        System.out.println("输出的xml=" + s);
        return s;
    }

既然是HTTPS请求,就需要绕过证书并击中web服务,使HTTPS SOAP请求绕过SSL证书。

public class ConnectHttps { 
    public static void main(String[] args) throws Exception { 
    /* 
* fix for 
* Exception in thread "main" javax.net.ssl.SSLHandshakeException: 
*  sun.security.validator.ValidatorException: 
*   PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: 
*    unable to find valid certification path to requested target 
*/ 
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) { } 
    } 
}; 
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); 
/* 
* end of the fix 
*/ 
    } 
}

客户端调用



String webServiceAndSoap = soapSpecialConnection(url,requestXml);System.out.println(webServiceAndSoap);


以上是https请求SOAP webService接口全部内容了。

相关文章
|
6天前
|
应用服务中间件 nginx
百度搜索:蓝易云【HTTP请求是如何关联Nginx server{}块的?】
总结来说,Nginx中的 `server{}`块用于关联HTTP请求和虚拟主机,通过配置不同的 `server{}`块,可以实现多个域名或IP地址的请求分发和处理。这样,Nginx可以根据不同的请求来提供不同的服务和内容。
40 0
|
6天前
|
JavaScript
Node.js【GET/POST请求、http模块、路由、创建客户端、作为中间层、文件系统模块】(二)-全面详解(学习总结---从入门到深化)(上)
Node.js【GET/POST请求、http模块、路由、创建客户端、作为中间层、文件系统模块】(二)-全面详解(学习总结---从入门到深化)
32 0
|
5天前
|
安全 Android开发
Android之OKHttp基本使用和OKHttp发送https请求安全认证
Android之OKHttp基本使用和OKHttp发送https请求安全认证
19 0
|
6天前
|
Web App开发 Java 测试技术
秒懂HTTPS接口(JMeter压测篇)
【5月更文挑战第11天】秒懂HTTPS接口(JMeter压测篇)
16 2
秒懂HTTPS接口(JMeter压测篇)
|
6天前
|
JavaScript
Node.js【GET/POST请求、http模块、路由、创建客户端、作为中间层、文件系统模块】(二)-全面详解(学习总结---从入门到深化)(下)
Node.js【GET/POST请求、http模块、路由、创建客户端、作为中间层、文件系统模块】(二)-全面详解(学习总结---从入门到深化)
31 0
|
6天前
|
安全 网络协议 算法
秒懂HTTPS接口(原理篇)
【4月更文挑战第24天】秒懂HTTPS接口(原理篇)
48 4
秒懂HTTPS接口(原理篇)
|
6天前
|
数据采集 缓存 前端开发
http和https请求服务器的时候在请求头部分都带什么到服务器呢?
HTTP和HTTPS请求头基本结构相似,HTTPS多了一层SSL/TLS加密。常见请求头如Accept(指定内容类型)、Authorization(身份验证)、Cookie(会话跟踪)、User-Agent(标识用户代理)等。HTTPS特有的头包括Upgrade-Insecure-Requests(升级到HTTPS)、Strict-Transport-Security(强制使用HTTPS)、Sec-Fetch-*(安全策略)和X-Content-Type-Options、X-Frame-Options等(增强安全性)。实际应用中,请求头会根据需求和安全策略变化。
25 0
|
6天前
|
Java API Spring
使用OkHttp在Spring Boot应用中发送HTTP请求
使用OkHttp在Spring Boot应用中发送HTTP请求
124 0
|
6天前
|
存储 缓存 安全
面试题:HTTP 协议包括哪些请求?
面试题:HTTP 协议包括哪些请求?
26 0
|
6天前
|
移动开发 自然语言处理 网络协议
Http解析实现/服务器Get请求的实现
Http解析实现/服务器Get请求的实现
52 0