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

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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值