调用已发布的WebService

WebService服务演示

  1.          登录http://www.webxml.com.cn

  2.           单击手机查询服务

  3.         选择要调用的方法 例如: getMobileCodeInfo.

  4.  输入要查询的手机号单击”调用” 截图如下, 免费用户 UserID为null

 

a)   可以看到返回如下结果:

<?xml version="1.0" encoding="utf-8" ?> 
  <string xmlns="http://WebXml.com.cn/">18323455678:重庆 重庆 重庆移动全球通卡</string>

 

 

  每种访问方式都有对应的说明,方法、参数与对应的返回数据。在点击方法名之后可以查看。一般四种。soap1.1,soap1.2,get,post。一般访问方式  HttpClient

Jar包:

1.Java方式访问WebService

(1)get方式:

说明:

 

url:  http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo  后面拼接参数

Java代码:

package ws_a;


import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;

/**调用第三方的webservice服务 ,获取电话号码信息
 * 
 */
public class MobileCodeService {
    //1. http-get方式访问webservice
    public void get(String mobileCode ,String userID ) throws Exception{
        URL url=new URL("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode="+mobileCode+
                "&userID="+userID);
        HttpURLConnection conn=(HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(5000);
        conn.setRequestMethod("GET");
        if(conn.getResponseCode()==HttpURLConnection.HTTP_OK){ //结果码=200
            InputStream is=conn.getInputStream();
            //内存流 ,  
            ByteArrayOutputStream boas=new ByteArrayOutputStream();
            byte[] buffer=new byte[1024];
            int len=-1;
            while((len=is.read(buffer))!=-1){
                boas.write(buffer, 0, len);
            }
            System.out.println("GET请求获取的数据:"+boas.toString());    
            boas.close();
            is.close();
        }
    }
    
    public static void main(String[] args) throws Exception{
        MobileCodeService ws=new MobileCodeService();
        ws.get("15110410513", "");

    }

}

 

结果;

 

(2)post方式

说明:

 

java代码:

package ws_a;


import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;

/**调用第三方的webservice服务 ,获取电话号码信息
 * 
 */
public class MobileCodeService {
    
    //2.Post请求 :通过Http-Client 框架来模拟实现 Http请求
    public void post(String mobileCode ,String userID) throws Exception{
        /**HttpClient访问网络的实现步骤:
         *  1. 准备一个请求客户端:浏览器 
         *  2. 准备请求方式: GET 、POST
         *  3. 设置要传递的参数
         *  4.执行请求
         *  5. 获取结果
         */
        HttpClient client=new HttpClient();
        PostMethod postMethod=new PostMethod("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo");
        //3.设置请求参数
        postMethod.setParameter("mobileCode", mobileCode);
        postMethod.setParameter("userID", userID);
        //4.执行请求 ,结果码
        int code=client.executeMethod(postMethod);
        //5. 获取结果
        String result=postMethod.getResponseBodyAsString();
        System.out.println("Post请求的结果:"+result);
    }

    public static void main(String[] args) throws Exception{
        MobileCodeService ws=new MobileCodeService();
        ws.post("13626217879", "");

    }

}

结果:

 (3)soap1.1访问

说明:

 

java代码:

package ws_a;


import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;

/**调用第三方的webservice服务 ,获取电话号码信息
 * 
 */
public class MobileCodeService {

    //2.Post请求 :通过Http-Client 框架来模拟实现 Http请求
    public void soap() throws Exception{

        HttpClient client=new HttpClient();
        PostMethod postMethod=new PostMethod("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx");
        //3.设置请求参数
          postMethod.setRequestBody(new FileInputStream("C:\\Users\\liqiang\\Desktop\\soap.xml")); 
          //修改请求的头部
          postMethod.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
        //4.执行请求 ,结果码
        int code=client.executeMethod(postMethod);
        System.out.println("结果码:"+code);
        //5. 获取结果
        String result=postMethod.getResponseBodyAsString();
        System.out.println("Post请求的结果:"+result);
    }

    public static void main(String[] args) throws Exception{
        MobileCodeService ws=new MobileCodeService();
        ws.soap();

    }

}

 

桌面的soap.xml

<?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>13834786998</mobileCode>
      <userID></userID>
    </getMobileCodeInfo>
  </soap:Body>
</soap:Envelope>

 

 

结果:

结果码:200
Post请求的结果:<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><getMobileCodeInfoResponse xmlns="http://WebXml.com.cn/"><getMobileCodeInfoResult>13834786998:山西 长治 山西移动全球通卡</getMobileCodeInfoResult></getMobileCodeInfoResponse></soap:Body></soap:Envelope>

 

 

     问题:1. 如何解析结果
              2. 如何传递对象参数

 

2.  推荐的访问方式  wsimport生成本地代理   URL是发布服务的地址(后缀加WSDL)


          在jdk 1.6 版本以后 ,通过jax-ws 包提供对webservice的支持
         - 该方式通过注解的方式来声明webservice
         - 通过 jdk EndPoint.publish()发布webserive服务

        早期的版本 : jax-rpc (remote produce call)

       webservice 纳入 w3c规范,其他的平台都支持该规范 :JEE\Php\.NET
        都支持wsimport 方式 : 对远程的webservice生成本地代理,再通过本地代理
                               来访问webservice
        - wsimport 命令的位置:
       C:\Program Files\Java\jdk1.7.0_04\bin


    

- 要求:
      1. jdk的 版本要在 jdk 1.6.21及以上
      2. 操作系统安装的jdk版本 与 MyEclispe 及 默认指定的版本要一致(因为导出的时候是用jdk的wsimport命令导出,所以在eclipse的运行环境需要与编译环境一致。)

    - wsimport使用:
      记得设置jdk\bin  环境变量  指定path
           语法  wsimport [opations] <wsdl_uri>
         - wsdl_uri:wsdl 的统一资源标识符
         - d  :指定要输出的文件的位置
         - s  :表示要解析java的源码 ,默认解析出的是class字节码
         - p  : 指定输出的包名

 

解析服务说明的源码:

1.解析到当前目录人家规定的包结构的class文件(dos窗口)

  cd Desktop

  wsimport http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?WSDL

因为有四种解析方式:soap1.1 soap1.2 get post ,所以其他三种出现警告,只能soap1.1解析成功

 

查看桌面:

 

 

 

 

 

注意:http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?WSDL是服务说明的地址

 

 

 

 

 

2.解析到指定目录指定包结构的带源码文件(dos窗口)

(1)下载服务说明的源码与字节码文件

C:\Users\liqiang\Desktop>wsimport -s ./ -p cn.qlq.c http://ws.webxml.com.cn/WebS
ervices/MobileCodeWS.asmx?WSDL

 

  

  可以把class文件打成jar包 jar cvf  test.jar 打包目录

(2)使用下载的源码与字节码

第一步:源码导入eclipse:

 

第二步:查看服务说明,确定入口类和方法

查看服务说明:(从下往上看)

服务类名:

 

 

服务方法名:

 

 

第三步:编写测试代码

测试代码:_Main.java

package cn.qlq.c;


import java.util.List;

public class _Main {
  /**通过wsimport生成本地代理 来访问 webservice服务端
   * 
   * @param args
   */
    public static void main(String[] args) {
        //生成服务对象
        MobileCodeWS ws=new MobileCodeWS();
        //取得webservice服务的访问方式  : Soap1.1  Soap 1.2  Http-get http-Post
        MobileCodeWSSoap mobileCodeWSSoap = ws.getMobileCodeWSSoap();
        /**
         *  返回的数据有两种类型 : 
         *  1. 简单的数据类型  。基本数据类型 :整数、布尔、字符串 等
         *  2. 复合的数据类型 :结构体 ,数组 ,对象 
         */
        //1.简单的数据
        String result=mobileCodeWSSoap.getMobileCodeInfo("13660559129", "");
        System.out.println("返回的结果:"+result);
        //2. 复合的数据  List<String> List<Student>
        ArrayOfString databaseInfo = mobileCodeWSSoap.getDatabaseInfo();
        List<String> results=databaseInfo.getString();
        //遍历集合
        for(String temp:results){
            System.out.println(temp);
        }
        //jsp  Select 
    }

}

 

ArrayOfString 是JDK中没有的类,在导出服务说明的时候自动带的。

运行结果:

 

注意:如果需要将传回来的数据进行对象的封装可以对字符串进行拆分处理。

 

posted @ 2017-09-19 22:31  QiaoZhi  阅读(806)  评论(0编辑  收藏  举报