4.2. 通过HttpURLConnection工具调取WebService接口

Demo代码:https://github.com/earnext/test-webservice
需求:客户端调取服务端WebService接口插入用户数据。
目录:

  1. WebService生成客户端代码两种方式
  2. WebService常用注解
  3. 编写服务端(发布服务)
    3.1. WebService服务端业务代码
    3.2. WebService服务端配置(发布服务)
  4. 编写客户端(调取接口)
    4.1. 通过Apache CXF生成客户端代码调取WebService接口
    4.2. 通过HttpURLConnection工具调取WebService接口
    4.3. 通过HttpClient工具调取WebService接口

1.HttpURLConnection工具类

/**
 * 发送webservice接口报文
 * @author Dongxibao
 * @date 2020-05-30
 */
@Slf4j
public class HttpURLConnectionXMLUtil {
     
    public static InputStream send(String wsdlUrl, String xmlMessage) throws IOException {
     
        if (StringUtils.isEmpty(wsdlUrl)) {
     
            return null;
        }
        int timeout = 600000;
        InputStream inputStream = null;
        log.info("[HttpURLConnection]初始化 wsdlUrl:{}", wsdlUrl);
        URL url = new URL(wsdlUrl);
        log.info("[HttpURLConnection]开始设置 http 参数");
        try {
     
            HttpURLConnection httpURLConnection = ((HttpURLConnection) url.openConnection());
            httpURLConnection.setInstanceFollowRedirects(false);
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setConnectTimeout(timeout);
            httpURLConnection.setReadTimeout(timeout);
            httpURLConnection.addRequestProperty("Content-Length",String.valueOf(xmlMessage.getBytes().length));
            httpURLConnection.addRequestProperty("Content-type","text/xml;charset=UTF-8");
            log.info("[HttpURLConnection]组装的请求报文:{}", new String(xmlMessage.getBytes()));
            DataOutputStream dataOutputStream = new DataOutputStream(httpURLConnection.getOutputStream());
            dataOutputStream.writeBytes(xmlMessage);
            dataOutputStream.flush();
            inputStream = httpURLConnection.getInputStream();
            // 中间存储,防止InputStream只能读取一次
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len;
            while ((len = inputStream.read(buffer)) > -1 ) {
     
                byteArrayOutputStream.write(buffer, 0, len);
            }
            byteArrayOutputStream.flush();
            InputStream in = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
            String result = IOUtils.toString(in, StandardCharsets.UTF_8);
            log.info("[HttpURLConnection]返回报文 :{}", result);
            // 返回原始的inputStream
            inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
        } catch (Exception e) {
     
            log.error("[HttpURLConnection]接口调用失败!");
            log.error("[HttpURLConnection]错误提示:{};  {}",e,e.getMessage());
        }
        return inputStream;
    }
}

2.调取

@Test
public void testInsertUser2() {
     
    // 拼装报文
    String strXml = "\n" +
            "  \n" +
            "    \n" +
            "      \n" +
            "        22\n" +
            "        test\n" +
            "      \n" +
            "    \n" +
            "  \n" +
            "";
    try {
     
        InputStream in = HttpURLConnectionXMLUtil.send(GlobalInterface.INSERT_USER,strXml);
    } catch (IOException e) {
     
        e.printStackTrace();
    }
}

你可能感兴趣的:(WebService,java,webservice)