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

本文介绍如何利用HttpURLConnection在Java中调用WebService接口。提供了详细的操作步骤,包括服务端业务代码编写、服务发布,以及客户端调用接口的三种方法,重点讲解了通过HttpURLConnection实现调取接口的过程。
摘要由CSDN通过智能技术生成

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调用第三方WebService接口的基本步骤: 1. 创建URL对象,设置目标WebService地址。 2. 使用URL对象创建HttpURLConnection对象。 3. 设置HttpURLConnection对象的请求方法为POST。 4. 设置HttpURLConnection对象的连接超时时间和读取超时时间。 5. 设置HttpURLConnection对象的请求头信息。 6. 设置HttpURLConnection对象的请求体信息。 7. 发送请求,获取响应。 8. 解析响应,获取需要的数据。 具体实现细节可以参考以下示例代码,供您参考: ``` URL url = new URL("http://xxxxxx:8080/xxxxx/xxxxx");//WebService地址 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST");//请求方式为POST conn.setConnectTimeout(5000);//连接超时时间为5秒 conn.setReadTimeout(5000);//读取超时时间为5秒 conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");//设置请求头信息 String soapXml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://service.xxx.com/\">" + "<soapenv:Header/>" + "<soapenv:Body>" + "<ser:xxxxx>" + "<ser:xxxxx>xxxxx</ser:xxxxx>" + "<ser:xxxxx>xxxxx</ser:xxxxx>" + "</ser:xxxxx>" + "</soapenv:Body>" + "</soapenv:Envelope>"; conn.setRequestProperty("Content-Length", String.valueOf(soapXml.getBytes().length));//设置请求体长度 conn.setDoOutput(true);//设置允许输出 OutputStream os = conn.getOutputStream(); os.write(soapXml.getBytes());//写入请求体信息 os.flush(); os.close(); if (conn.getResponseCode() == 200) {//获取响应码 InputStream is = conn.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line); } is.close(); reader.close(); String result = sb.toString();//获取响应数据 //解析响应数据,获取需要的数据 } else { //请求失败处理逻辑 } conn.disconnect();//关闭连接 ``` 需要注意的是,以上示例代码中的SOAP请求体格式是一种常见的WebService请求格式,如果需要调用其他格式的WebService接口,请求体格式可能会有所不同。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值