WebService-----------调用的几种方式

第一种:客户端方式wsimport 【掌握】

案例:手机号码归属地查询WEB服务

案例:天气查询WEB服务

第二种:Service方式 【了解】

案例:天气查询WEB服务

第三种:HTTPURLConnection方式【代码自己发送http请求-掌握】

案例:手机号码归属地查询WEB服务

第四种:Ajax调用方式【不要求掌握,原理与第三种一样,只不过是用js写】

....

 

 

注意:天气预报的webservice有两个,案例演示,自己选择

http://ws.webxml.com.cn/WebServices/WeatherWS.asmx

包含0230个以上中国城市和100个以上国外城市天气预报数据

 

http://ws.webxml.com.cn/WebServices/WeatherWebService.asmx

包括 340 多个中国主要城市和 60 多个国外主要城市三日内的天气预报数据。

5.1 第一种:客户端方式【推荐使用】

案例:手机归属地查询

公网服务地址http://www.webxml.com.cn/zh_cn/index.aspx

手机归属地Web服务 http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl

 

步骤

第一步:wsimport生成客户端代码

wsimport -p com.gyf.mobile.ws -s . http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl

 

第二步:阅读使用说明书,使用生成客户端代码调用服务端

第三步: 获取指定电话号码的归属地信息,代码如下

WebService-----------调用的几种方式

 

 

 

案例:天气查询WEB服务

公网服务地址http://www.webxml.com.cn/zh_cn/index.aspx

天气Web服务 http://ws.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl

 

步骤

第一步:wsimport生成客户端代码【此命令放在你在工程src目录下执行】

wsimport -p com.gyf.weather.ws -s . http://ws.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl

 

使用上面的命令时会出现失败,出现『则很可能需要更改 ‘s:schema’ 的前缀』错误

WebService-----------调用的几种方式

 

解决办法是,将wsdl另存为一个xml文件,然后打开文件,把s:schema内容删除

WebService-----------调用的几种方式 是把整个s:element标签删除

在终端执行下面命令wsimport -p com.gyf.weather.ws -s . +xml文件路径

如果是window,路径写 file://c:/…

如果是linux/mac 直接写 根路径/…

WebService-----------调用的几种方式

 

 

 

第二步:阅读使用说明书,使用生成客户端代码调用服务端

第三步: 根据城市获取天气信息

WebService-----------调用的几种方式

 

5.2 第二种:Service方式【了解】

案例:天气查询WEB服务

public static void main(String[] args) throws Exception {

            //说明书地址

            URL url = new URL("http://ws.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl");

            //String namespaceURI 命名空间

            //String localPart 服务名称

            QName qName = new QName("http://WebXml.com.cn/","WeatherWebService");

            //创建服务

            Service service = Service.create(url,qName);

            //获取服务端点

            WeatherWebServiceSoap soap = service.getPort(WeatherWebServiceSoap.class);

     

            for(String item : soap.getSupportProvince().getString()){

                  System.out.println(item);

            }

      }

 

5.3 第三种:HTTPURLConnection方式

案例:手机号码归属地查询WEB服务

根据http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?op=getMobileCodeInfo说明的soap1.1请求格式来实现即可

使用java.net.HttpURLConnection发送POST网络请求

 

注意事项

设置post请求方式时,post一定要大写

默认HttpURLConnection是没有权限写数据,需要设置setDoOutput(true)

public class Demo2 {

 

    public static void main(String[] args) throws Exception {

         //创建url

         URL url = new URL("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx");

         HttpURLConnection conn = (HttpURLConnection) url.openConnection();

         conn.setRequestMethod("POST");

         //设置可以写请求数据

         conn.setDoOutput(true);

        

         //设置请求头

         conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");

        

        

         OutputStream os = conn.getOutputStream();

         os.write(getXML("13633123359").getBytes());

        

         int code = conn.getResponseCode();

         System.out.println("响应状态码:" + code);

        

         if(code == 200){

             //读取响应数据

             InputStream is = conn.getInputStream();

             InputStreamReader reader = new InputStreamReader(is,"UTF-8");

             BufferedReader br = new BufferedReader(reader);

             String line = null;

             while((line = br.readLine()) != null){

                  System.out.println(line);

             }

             br.close();

         }

    }

   

    public static String getXML(String tel){

         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>" + tel + "</mobileCode>" +

                  "<userID></userID>" +

                  "</getMobileCodeInfo>" +

                  "</soap:Body>" +

                  "</soap:Envelope>";

    }

}

 

 

案例:天气查询WEB服务

  1. 注解修改WSDL内容

WebService-----------调用的几种方式

WebService-----------调用的几种方式

 

 

案例:

WebService-----------调用的几种方式