关于webservice的调用

webservice介绍

webService是一种使用http传输SOAP协议数据的远程调用技术。 通过SOAP在Web上提供的软件服务,使用WSDL文件进行说明,并通过UDDI进行注册。 发布一般都是使用WSDL(web service descriptive language)文件的样式来发布的,在WSDL文件里面,包含这个webservice暴露在外面可供使用的接口。

wsdl

WSDL(网络服务描述语言,Web Services Description Language)即web服务描述语言,它是服务端的使用说明书,是XML格式的文档,说明服务地址、服务类、方法、参数和返回值,是伴随服务发布成功,自动生成的。

  • 服务视图,webservice的服务结点,它包括了服务端点

  • 为每个服务端点定义消息格式和协议细节

  • 服务端点,描述 web service可被执行的操作方法,以及相关的消息,通过binding指向portType

  • 定义一个操作(方法)的数据参数(可有多个参数)

  • 定义 web service 使用的全部数据类型

webservice客户端调用

1.使用wsimport来生成客户端代码、

wsimport命令是jdk提供的,作用是根据使用说明书生成客户端代码;

  • -d:默认参数,用于生成class文件;
  • -s:生成java文件;
  • -p:指定java文件的包名,不指定则为WSDL说明书中namespace的值倒写;
  • -keep:保存java文件
wsimport -p com.joker -keep -s . http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl
复制代码

会在当前目录生成java代码。

编写测试:

 public static void main(String[] args) {
       //创建服务视图
       MobileCodeWS mobileCodeWS = new MobileCodeWS();
       //获取服务实现类
       MobileCodeWSSoap mobileCodeWSSoap = mobileCodeWS.getPort(MobileCodeWSSoap.class);
       //调用查询方法
       String reuslt = mobileCodeWSSoap.getMobileCodeInfo("18866666666", null);
       System.out.println(reuslt);
   }
复制代码

2.http请求

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * <p>Title: HttpClient.java</p>
 * <p>Description:HttpURLConnection调用方式</p>
 */
public class HttpClient {

    public static void main(String[] args) throws IOException {
        //第一步:创建服务地址,不是WSDL地址
        URL url = new URL("http://ws.webxml.com.cn/WebServices/MobileCodeWS.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 = getXML("15845983532");
        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();
    }


    private String getResult(String info){

        return "";
    }

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

}
复制代码

3.动态调用

    public static void main(String[] args) throws IOException {
        //创建WSDL的URL,注意不是服务地址
        URL url = new URL("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl");

        //创建服务名称
        //1.namespaceURI - 命名空间地址
        //2.localPart - 服务视图名
        QName qname = new QName("http://WebXml.com.cn/", "MobileCodeWS");

        //创建服务视图
        //参数解释:
        //1.wsdlDocumentLocation - wsdl地址
        //2.serviceName - 服务名称
        Service service = Service.create(url, qname);
        //获取服务实现类
        MobileCodeWSSoap mobileCodeWSSoap = service.getPort(MobileCodeWSSoap.class);
        //调用查询方法
        String result = mobileCodeWSSoap.getMobileCodeInfo("18866666666", "");
        System.out.println(result);
    }
复制代码

当然,各种IDE也支持webservice的代码generate.后续待补...

转载于:https://juejin.im/post/5c0e5406f265da61561f190a

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值