通过HttpsURLConnection 调用 webService 接口

public static void main(String [] args){
 try {
               Map<String,String> map = new HashMap<>();
               map.put("param1","324");
               map.put("param2","56");  

                //转化
                String json = mapper.writeValueAsString(map);

                System.out.println("邮件json:" + json); //以上为 
                //调用

                // 设置WebService的WSDL地址
                String wsdlUrl = "https://test.edu.cn/mp/service/EmailService";
                // 创建URL对象
                URL url = new URL(wsdlUrl);
                 //这里解决调用需要证书的问题
                TrustManager[] trustAllCerts = new TrustManager[]{
                        new X509TrustManager() {
                            public X509Certificate[] getAcceptedIssuers() {
                                return null;
                            }

                            public void checkClientTrusted(X509Certificate[] certs, String authType) {
                            }

                            public void checkServerTrusted(X509Certificate[] certs, String authType) {
                            }
                        }
                };


// 设置请求的数据
                String payload = getXml(json);


                final OutputStream outputStream;

                SSLContext sc = SSLContext.getInstance("TLS");
                sc.init(null, trustAllCerts, new SecureRandom());
                HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
                //这里解决调用需要证书的问题
                // 打开连接
                HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();

                // 设置请求方法
                connection.setRequestMethod("POST");

                // 设置通用的请求属性
                connection.setRequestProperty("Content-Type", "text/xml;charset=utf-8");
                connection.setRequestProperty("SOAPAction", "");
                // 设置输入输出权限,connection默认没有
                connection.setDoInput(true);
                connection.setDoOutput(true);

                outputStream = connection.getOutputStream();
                outputStream.write(payload.getBytes(StandardCharsets.UTF_8));
                // 执行发送,获取响应数据
                final InputStream inputStream = connection.getInputStream();
                final InputStreamReader reader = new InputStreamReader(inputStream);
                final BufferedReader bufferedReader = new BufferedReader(reader);
                StringBuilder sb = new StringBuilder();
                String temp = null;
                while (null != (temp = bufferedReader.readLine())) sb.append(temp);
                System.out.println(sb);

                bufferedReader.close();
                reader.close();
                inputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
} 
private static String getXml(String json) {
    String[] templates = {
            "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n",
            "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ema=\"http://email.webservice.com\">\n",
            "    <soapenv:Header/>\n",
            "    <soapenv:Body>\n",
            "      <ema:saveEmail>\n",
            "      <ema:in0>",json,"</ema:in0>\n",
            "      </ema:saveEmail>\n",
            "</soapenv:Body>\n",
            "</soapenv:Envelope>\n"
    };
    final StringBuffer buffer = new StringBuffer();
    Arrays.asList(templates).forEach(buffer::append);
    return buffer.toString();
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值