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;
    }
}
相关推荐
Dolphin_Home5 分钟前
轻量实用的 XML 与 JSON / 对象互转工具类(Jackson 实现)
xml·java·json
咖啡教室16 分钟前
每日一个计算机小知识:DHCP
后端·网络协议
Yeniden23 分钟前
【设计模式】# 外观模式(Facade)大白话讲解!
java·设计模式·外观模式
脚踏实地的大梦想家23 分钟前
【Go】P17 Go语言并发编程核心:深入理解 Goroutine (从入门到实战)
java·开发语言·golang
Yeniden23 分钟前
【设计模式】 组合模式(Composite)大白话讲解
java·设计模式·组合模式
初学小白...28 分钟前
线程同步机制及三大不安全案例
java·开发语言·jvm
咖啡教室31 分钟前
每日一个计算机小知识:ARP协议
后端·网络协议
CS Beginner1 小时前
【搭建】个人博客网站的搭建
java·前端·学习·servlet·log4j·mybatis
李慕婉学姐1 小时前
Springboot旅游管理系统8cx8xy5m(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库·spring boot·旅游
JavaTree20171 小时前
【Spring Boot】Spring Boot解决循环依赖
java·spring boot·后端