系统之间接口访问

对于外部系统发布的webservice接口WSDL。可使用HTTP请求方式去访问。

/**
	 * http post请求
	 * @param urlString 请求地址
	 * @param soapXML xml字符串
	 * @return
	 */
    public static String HttpPostRequest(String urlString , String soapXML){
        String result = "";
        HttpURLConnection connection = null ;
        try{
            //1:创建服务地址
            URL url = new URL(urlString);
            //2:打开到服务地址的一个连接
            connection = (HttpURLConnection) url.openConnection();
            //3:设置连接参数
            //3.1设置发送方式:POST必须大写
            connection.setRequestMethod("POST");
            //3.2设置数据格式:Content-type
            connection.setRequestProperty("content-type", "text/xml;charset=UTF-8");
            //3.3设置输入输出,新创建的connection默认是没有读写权限的,
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.connect();//打开到此 URL 引用的资源的通信链接
            //4:组织SOAP协议数据,发送给服务端
            LOGGER.debug( "调接口xml为:"+soapXML);
            OutputStream os = connection.getOutputStream();
            os.write(soapXML.getBytes("UTF-8"));//使用编码UTF-8
            os.flush();//刷新输出缓冲区

            //5:接收服务端的响应
            int responseCode = connection.getResponseCode();
            LOGGER.debug("responseCode:" + responseCode);
            StringBuilder sb = new StringBuilder();
            if(responseCode == 200){//表示服务端响应成功
                InputStream is = connection.getInputStream();
                InputStreamReader isr = new InputStreamReader(is, "UTF-8");
                BufferedReader br = new BufferedReader(isr);
                String temp = null;
                while(null != (temp = br.readLine())){
                    sb.append(temp);
                }
                LOGGER.debug("接口返回的xml为:"+sb.toString());
                result = sb.toString();
                is.close();
                isr.close();
                br.close();
            }else{
                result = "请求返回码为:"+responseCode+"";
            }
            os.close();
            connection.disconnect();
        }catch (Exception ex){
            ex.printStackTrace();
            eservice.saveException(ex);
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
        }
        return result ;
    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值