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;
    }
}
相关推荐
时艰.1 分钟前
JVM 垃圾收集器ParNew&CMS与三色标记算法
java·jvm·算法
百***07452 分钟前
小米MiMo-V2-Flash深度解析:国产开源大模型标杆+一步API接入全指南
java·大数据·开源·php
信也科技布道师3 分钟前
基石Redis实例自动化调度之路
java·开发语言·redis·自动化
码农幻想梦4 分钟前
实验九 Restful和ajax实现
后端·ajax·restful
我就是你毛毛哥12 分钟前
Spring Boot 项目使用 EasyExcel 实现导出功能
数据库·spring boot·oracle
666HZ66617 分钟前
程序设计竞赛java
java·开发语言
三不原则18 分钟前
AIOps 技术架构全景:数据采集→分析→自动化执行全流程
java·架构·自动化
今天多喝热水23 分钟前
SpEL(Spring Expression Language) 表达式
java·后端·spring
wasp52024 分钟前
Hudi 客户端实现分析
java·开发语言·人工智能·hudi