springboot调用webservice简便方式

此方法不建议在需要高并发或者是批量调用webservice接口时使用,比较吃内存。仅在管理系统后台中,或者是用户量少时可以采用此取巧方案。

直接上核心代码:

java 复制代码
package com.dhc.minboot.api;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * xx的webservice调用专用类
 */
@Component
public class HRClient {

    private static final Logger log = LoggerFactory.getLogger(HRClient.class);

    public Boolean doPost(String info) throws Exception {
        Boolean isSuccess = false;
        String wsUrl = "http://xxx.xx.xx.xxx/public/WEBHR_Service.asmx";
        URL url = new URL(wsUrl);
        URLConnection conn1 = url.openConnection();
        HttpURLConnection con = (HttpURLConnection) conn1;
        con.setDoInput(true);
        con.setDoOutput(true);
        con.setRequestMethod("POST");
        con.setRequestProperty("content-type","text/xml");
        con.setRequestProperty("SOAPAction","http://tempuri.org/BestSignPushUrl");
        String requestBody = "<soap:Envelope " +
                "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"> " +
                "<soap:Body>" +
                "<BestSignPushUrl xmlns=\"http://tempuri.org/\">" +
                "<httpBody>" + info +"</httpBody>" +
                "</BestSignPushUrl>" +
                " </soap:Body>" +
                "</soap:Envelope>";
        OutputStream out = con.getOutputStream();
        out.write(requestBody.getBytes());
        out.close();
        try {
            int code1 = con.getResponseCode();
            if(code1==200){
                InputStream is = con.getInputStream();
                byte[] b = new byte[1024];
                StringBuffer sb = new StringBuffer();
                int len = 0;
                while((len=is.read(b))!=-1){
                    String str = new String(b,0,len,"UTF-8");
                    sb.append(str);
                }
                log.info("hr系统请求返回{}",sb.toString());
                isSuccess = true;
                is.close();
                con.disconnect();
            } else {
                log.info("hr系统请求出现问题,code为{},参数为{}",code1,info);
            }
        } catch (Exception e) {
            log.error("hr系统请求出现问题,参数{}",info);
        }
        return isSuccess;
    }
}
相关推荐
Asthenia04121 分钟前
面试复盘:使用 perf top 和火焰图分析程序 CPU 占用率过高
后端
Asthenia04122 分钟前
面试复盘:varchar vs char 以及 InnoDB 表大小的性能分析
后端
Asthenia04122 分钟前
面试问题解析:InnoDB中NULL值是如何记录和存储的?
后端
王磊鑫3 分钟前
重返JAVA之路-初识JAVA
java·开发语言
半兽先生25 分钟前
WebRtc 视频流卡顿黑屏解决方案
java·前端·webrtc
Asthenia041228 分钟前
面试官问我:TCP发送到IP存在但端口不存在的报文会发生什么?
后端
Asthenia041229 分钟前
HTTP 相比 TCP 的好处是什么?
后端
Asthenia041230 分钟前
MySQL count(*) 哪个存储引擎更快?为什么 MyISAM 更快?
后端
Asthenia041232 分钟前
面试官问我:UDP发送到IP存在但端口不存在的报文会发生什么?
后端
Asthenia041232 分钟前
深入理解 TCP backlog 参数:意义、应用场景与 Netty 中的应用
后端