java根据WSDL文档,调用WebService

查看接口信息

当我们获取到了对方提供的wsdl地址,首先可以在网页上能够访问wsdl文档。

使用soapui工具查看接口信息

下载soapui调试工具 http://www.downcc.com/soft/15465.html
例如:
http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?wsdl
这个是查询ip归属地的接口
在soapui中新建一个SOAP工程,然后copy上面的WSDL地址,这样可以查看这个服务有多少接口,然后show Request就可以看到怎么调用的xml文件内容,可以在此测试一下是否正常。
SOAP客户端不需要使用第三方控件,只要按XML文件的内容拼出字符串就可以了。

Java调用示例

package my;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class SoapTest {

    public static void main(String[] args) throws IOException {
        test2();
    }

    public static void test1(String phone) throws IOException {
        // 第一步:创建服务地址
        URL url = new URL("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl");
        // 第二步:打开一个通向服务地址的连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        // 第三步:设置参数
        // 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);

        // 第四步:组织SOAP数据,发送请求
        String soapXML = getXML(phone);
        // 将信息以流的方式发送出去
        OutputStream os = connection.getOutputStream();
        os.write(soapXML.getBytes());
        // 第五步:接收服务端响应,打印
        int responseCode = connection.getResponseCode();
        if (200 == responseCode) {// 表示服务端响应成功
            // 获取当前连接请求返回的数据流
            InputStream is = connection.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);

            StringBuilder sb = new StringBuilder();
            String temp = null;
            while (null != (temp = br.readLine())) {
                sb.append(temp);
            }

            /**
             * 打印结果
             */
            System.out.println(sb.toString());

            is.close();
            isr.close();
            br.close();
        }
        os.close();
    }

    public static String getXML(String phone) {
        String soapXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                + "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2003/XMLSchema-instance\" "
                + "xmlns:web=\"http://WebXml.com.cn/\"  " + "xmlns:xsd=\"http://www.w3.org/2003/XMLSchema\" "
                + "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" + "<soap:Body>"
                + "<web:getMobileCodeInfo>" + phone + "</web:getMobileCodeInfo>" + "</soap:Body>" + "</soap:Envelope>";
        return soapXML;
    }


    public static void test2() throws IOException {
        // 第一步:创建服务地址
        URL url = new URL("http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx");
        // 第二步:打开一个通向服务地址的连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        // 第三步:设置参数
        // 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);

        // 第四步:组织SOAP数据,发送请求
        String soapXML = getXML2();
        // 将信息以流的方式发送出去
        OutputStream os = connection.getOutputStream();
        os.write(soapXML.getBytes());
        // 第五步:接收服务端响应,打印
        int responseCode = connection.getResponseCode();
        if (200 == responseCode) {// 表示服务端响应成功
            // 获取当前连接请求返回的数据流
            InputStream is = connection.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);

            StringBuilder sb = new StringBuilder();
            String temp = null;
            while (null != (temp = br.readLine())) {
                sb.append(temp);
            }

            /**
             * 打印结果
             */
            System.out.println(sb.toString());

            is.close();
            isr.close();
            br.close();
        }
        os.close();
    }


    public static String getXML2() {
        String soapXML = "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:web=\"http://WebXml.com.cn/\"> "
                + "<soap:Header/>" + "<soap:Body>" + "<web:getCountryCityByIp>"
                + "<web:theIpAddress>223.202.116.5</web:theIpAddress>" + "</web:getCountryCityByIp>" + "</soap:Body>"
                + "</soap:Envelope>";
        return soapXML;
    }
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值