基于拼接SOAP请求第三方接口工具类(简单案例)

一丶前言(soap协议的了解):

SOAP:简单对象访问协议,简单对象访问协议(SOAP)是一种轻量的、简单的、基于 XML 的协议,它被设计成在 WEB 上交换结构化的和固化的信息。 SOAP 可以和现存的许多因特网协议和格式结合使用,包括超文本传输协议( HTTP),简单邮件传输协议(SMTP),多用途网际邮件扩充协议(MIME)。它还支持从消息系统到远程过程调用(RPC)等大量的应用程序。SOAP协议可以简单地理解为:SOAP=RPC+HTTP+XML,即采用HTTP作为通信协议,RPC(Remote Procedure Call Protocol 远程过程调用协议)作为一致性的调用途径,XML作为数据传送的格式,从而允许服务提供者和服务客户经过防火墙在Internet上进行通信交互。

二丶HTTP拼接soap(xml)请求接口
/**
     * webservice-asmx post请求接口
     *
     * @param urlString         服务器地址
     * @param soapActionString  方法名
     * @param xml               解析的xml
     * @throws IOException
     */
    public static void sendServiceAsmxPost(String urlString, String soapActionString, String xml) throws IOException {
        // 获取连接对象
        URL url = new URL(urlString);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        // 拼接soap
        byte[] buf = xml.getBytes(StandardCharsets.UTF_8);
        // 设置报头
        conn.setRequestProperty("Content-Length", String.valueOf(buf.length));
        conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
        conn.setRequestProperty("soapActionString", soapActionString);
        conn.setRequestMethod("POST");
        // 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;
            while ((len = is.read(b)) != -1) {
                String s = new String(b, 0, len, StandardCharsets.UTF_8);
                sb.append(s);
            }
            is.close();
        }
        System.out.println(sb);
    }
三丶调用工具类测试
@ResponseBody
    @RequestMapping("sendMessage")
    public JsonResult sendMessage() {

        String url = "http://171.168.1.29:8765/WebService.asmx";
        String method = "SendMessage";
        String xml = "<?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:Header>\n" +
                "    <MySoapHeader xmlns=\"http://tempuri.org/\">\n" +
                "      <Password></Password>\n" +
                "    </MySoapHeader>\n" +
                "  </soap:Header>\n" +
                "  <soap:Body>\n" +
                "    <SendMessage xmlns=\"http://tempuri.org/\">\n" +
                "      <phone></phone>\n" +
                "      <message></message>\n" +
                "      <taskname></taskname>\n" +
                "      <hospMessagid></hospMessagid>\n" +
                "    </SendMessage>\n" +
                "  </soap:Body>\n" +
                "</soap:Envelope>";
        try {
            HttpRequestUtil.sendServiceAsmxPost(url, method, xml);
        }catch (IOException e) {
            e.printStackTrace();
        }
        return new JsonResult("SUCCESS");
    }
四丶注意事项

1丶第三方提供的接口协议是以上述一样才能使用
2丶此方法上是使用了soap:heard请求头进行接口验证的, 要根据自己的xml进行修改与测试

测试是成功的, 因有其他原因无法提供成功的图,有什么问题请下方留言一起探讨与解决

猜你喜欢

转载自blog.csdn.net/spring_is_coming/article/details/109103070
今日推荐