WebService 创建客户端访问服务端面的三种方式

来源:互联网 发布:网络会计培训班 编辑:程序博客网 时间:2024/05/21 11:35

WebService 创建客户端访问服务端面的三种方式:
反正我感觉第三种不好用。。。写起来太麻烦

第三种:HttpURLConnection方式

    /**     * WebService第三种客户端访问服务创建方式     */    public static void getIphoneByHttpConnection() throws IOException{        //创建服务器地址,不是wsdl的地址        URL url = new URL("http://ws.webxml.com.cn/WebServices/WeatherWS.asmx");        //打开个通向服务器地址的连接        HttpURLConnection openConnection = (HttpURLConnection) url.openConnection();        //设置参数        openConnection.setDoOutput(true);       //设置输出权限        openConnection.setDoInput(true);            //设置写入权限        openConnection.setRequestProperty("content-type", "text/xml;charset=utf-8");    //设置数据格式         openConnection.setRequestMethod("POST");    //设置请求方式        //组装XML数据        String result = getXmlInfo("18888545655");        //获取输出流        OutputStream outputStream = openConnection.getOutputStream();        //将组装好的XML转换成byte数组发送到服务上        outputStream.write(result.getBytes());        //接收服务器返回值        int responseCode = openConnection.getResponseCode();        //如果成功        if(responseCode == 200){            //获取响应的流            InputStream inputStream = openConnection.getInputStream();            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);            //接收读取返回的响应信息            StringBuffer resultVal = new StringBuffer();            String temp = "";            //循环读取数据            while ((temp = bufferedReader.readLine()) != null) {                resultVal.append(temp);            }            //输出数据            System.out.println(resultVal);        }else {            System.out.println("服务器出错");        }    }    public static String getXmlInfo(String iphone){        String result = "<?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>"+iphone+"</mobileCode>"                  +"<userID> </userID>"                +"</getMobileCodeInfo>"              +"</soap:Body>"            +"</soap:Envelope>";        return result;    }

第二种:Service方式

/**     *WebService第二种客户端访问服务创建方式     */    public static void getWeather() throws MalformedURLException{        //创建URL        URL wsdlDocumentLocation = new URL("http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl");        //创建ServiceName,第一个参数:命令空间(targetNamespace的值),第二参数:service视图名        QName serviceName = new QName("http://WebXml.com.cn/", "WeatherWS");        //创建Service视图        Service create = Service.create(wsdlDocumentLocation, serviceName);        //通过port获取实际的调用 对象        WeatherWSSoap port = create.getPort(WeatherWSSoap.class);        //调用 方法,获取值        ArrayOfString weather = port.getWeather("周口", "");        //解析值,并输出        List<String> string = weather.getString();        for (String string2 : string) {            System.out.println(string2);        }    }

第一种方式 :

 public static void main(String[] args) {        //创建WebServer服务发布的对象        WeatherServiceImplService weatherServiceImplService = new WeatherServiceImplService();        //通过WebServer的对象获取Port,参数是wsdl文件中指定的类        WeatherServiceImpl port = weatherServiceImplService.getPort(WeatherServiceImpl.class);        //远程调用WebService服务器的接口,获取数据        String weatherInfo = port.getWeatherInfo("河南");        //打印获取 的数据        System.out.println("服务端回复:"+weatherInfo);    }
阅读全文
0 0
原创粉丝点击