Java调用WebService实例

    /**
     * 检查QQ是否在线
     */
    @Test
    public void qqCheckOnline(){
        String endpoint = "http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx?wsdl";
        Service service = new Service();
        try {
            Call call = (Call) service.createCall();
            call.setTargetEndpointAddress(new java.net.URL(endpoint));
            call.setSOAPActionURI("http://WebXml.com.cn/qqCheckOnline");
            call.setOperationName(new QName("http://WebXml.com.cn/", "qqCheckOnline"));
            call.addParameter(new QName("http://WebXml.com.cn/", "qqCode"),
                    org.apache.axis.encoding.XMLType.XSD_STRING, ParameterMode.IN);
            call.setReturnType(XMLType.SOAP_STRING);
            System.out.println(call.invoke(new Object[]{"160468630*"}));
        }catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 获取天气
     */
    @Test
    public void getWeather(){
        String endpoint = "http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl";
        Service service = new Service();
        try {
            Call call = (Call) service.createCall();
            call.setTargetEndpointAddress(new URL(endpoint));
            call.setSOAPActionURI("http://WebXml.com.cn/getWeather");
            call.setOperationName(new QName("http://WebXml.com.cn/", "getWeather"));
            call.addParameter(new QName("http://WebXml.com.cn/", "theCityCode"),
                    org.apache.axis.encoding.XMLType.XSD_STRING, ParameterMode.IN);
            call.addParameter(new QName("http://WebXml.com.cn/", "theUserID"),
                    org.apache.axis.encoding.XMLType.XSD_STRING, ParameterMode.IN);
            call.setReturnType(XMLType.SOAP_VECTOR);
            System.out.println(call.invoke(new Object[]{"",""}));
        }catch (Exception e) {
            e.printStackTrace();
        }

    }
posted @ 2021-11-26 15:33  晴和琼林  阅读(409)  评论(0编辑  收藏  举报