构建SOAP连接、解析xml格式响应内容

public static SOAPMessage createWSRequest(String ip, String soapMethod, String id, String idType) {
    //soap请求构建
    String url = WEBSERVICE_URL;
    url = url.replace(IP_IDENTIFY, ip);

    try {
        SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
        SOAPConnection connection = soapConnectionFactory.createConnection();
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage message = messageFactory.createMessage();
        SOAPPart soapPart = message.getSOAPPart();
        SOAPEnvelope envelope = soapPart.getEnvelope();
        envelope.addNamespaceDeclaration("ns", "http://service.medc.com/");
        SOAPBody body = envelope.getBody();
        SOAPElement bodyElement = body.addChildElement(soapMethod, "ns");
        //方法传参
        bodyElement.addChildElement(idType).addTextNode(id);
        //响应获取
        return connection.call(message, url);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
//返回xml数据
public static String transformToString(Source source) {
    StringWriter writer = new StringWriter();
    try {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.transform(source, new StreamResult(writer));
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return writer.toString();
}

public static int parseType(String xml, List list, String objType) {
    String elementResponseName = "";
    //根据方法不同,定义解析的方法响应
    if (objType.equals("order")) {
        elementResponseName = "getMedicalOrdersResponse";
        objType = "order";
    } else if (objType.equals("patient")) {
        elementResponseName = "getPatientInfoResponse";
        objType = "patient";
    }else if (objType.equals("nurse")) {
        elementResponseName = "getNurseNameResponse";
        objType = "nurse";
    }

    String code = String.valueOf(UNKNOWN_ERROR);
    try {
        if (null != xml) {
            Document document = DocumentHelper.parseText(xml);
            //获取根节点
            Element root = document.getRootElement();
            //获取根节点下的tag1标签
            Element tag1 = root.element("Body");
            code = tag1.element(elementResponseName).element("return").element("code").getText();
            if (String.valueOf(SUCCESS).equals(code)) {
                String rs = tag1.element(elementResponseName).element("return")
                        .element("data").getText();
                if (objType.equals("order")) {
                    list.add(JSON.parseObject(rs, AllDataDTO.DoctorOrderInfoDTO.class));
                } else if (objType.equals("patient")) {
                    list.add(JSON.parseObject(rs, AllDataDTO.PatientInfoDTO.class));
                } else if (objType.equals("nurse")) {
                    list.add(rs);
                }

            }
        }

    } catch (Exception e) {
        e.printStackTrace();
        log.error("解析响应异常", e);
    }
    return Integer.parseInt(code);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要向 WebService 发送 XML 格式的请求,需要使用以下步骤: 1. 构建 XML 请求消息体:根据 WebService 的接口文档,构建符合其要求的 XML 请求消息体。 2. 创建 HTTP 连接:使用 HttpClient 或 HttpURLConnection 等库创建 HTTP 连接,设置请求的 URL 和请求方法为 POST。 3. 设置请求头:设置 Content-Type 为 text/xml;charset=UTF-8,表示请求体为 XML 格式。 4. 发送请求:将请求消息体通过 HTTP 连接发送出去。 5. 接收响应:等待 WebService 的响应解析响应消息体,获取返回的数据。 以下是一个 Java 实现的示例代码: ```java import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class WebServiceClient { public static void main(String[] args) throws Exception { // 构建 XML 请求消息体 String requestXml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://example.com/service/\"><soapenv:Header/><soapenv:Body><ser:getData><arg0>123</arg0></ser:getData></soapenv:Body></soapenv:Envelope>"; // 创建 HTTP 连接 URL url = new URL("http://example.com/service"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); // 设置请求头 conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8"); // 发送请求 conn.setDoOutput(true); conn.getOutputStream().write(requestXml.getBytes("UTF-8")); // 接收响应 BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } reader.close(); } } ``` 注意:以上代码仅作为示例,实际使用时需要根据具体的 WebService 接口文档进行相应的修改。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值