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;
    }
}
相关推荐
向前看-16 分钟前
验证码机制
前端·后端
xlsw_17 分钟前
java全栈day20--Web后端实战(Mybatis基础2)
java·开发语言·mybatis
神仙别闹1 小时前
基于java的改良版超级玛丽小游戏
java
黄油饼卷咖喱鸡就味增汤拌孜然羊肉炒饭2 小时前
SpringBoot如何实现缓存预热?
java·spring boot·spring·缓存·程序员
暮湫2 小时前
泛型(2)
java
超爱吃士力架2 小时前
邀请逻辑
java·linux·后端
南宫生2 小时前
力扣-图论-17【算法学习day.67】
java·学习·算法·leetcode·图论
转码的小石2 小时前
12/21java基础
java
李小白662 小时前
Spring MVC(上)
java·spring·mvc
GoodStudyAndDayDayUp2 小时前
IDEA能够从mapper跳转到xml的插件
xml·java·intellij-idea