Java调用webservice

  • Post author:
  • Post category:java




Java调用webservice



1、webService依赖

        <!-- webService依赖-->
        <dependency>
            <groupId>wsdl4j</groupId>
            <artifactId>wsdl4j</artifactId>
            <version>1.6.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis</groupId>
            <artifactId>axis</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>commons-discovery</groupId>
            <artifactId>commons-discovery</artifactId>
            <version>0.2</version>
        </dependency>
        <dependency>
            <groupId>axis</groupId>
            <artifactId>axis-jaxrpc</artifactId>
            <version>1.4</version>
        </dependency>



2、调用方式



1、方式一

    @GetMapping("/getWeb1")
    public String getWeb1() throws ServiceException, RemoteException {
        //获取webservice接口地址
        String endpoint = "http://172.10.10.143:44345/MainInst.asmx";
        //获取域名地址,server定义的
        String soapaction = "http://tempuri.org/";
        //调用的方法名
        String method = "GetVal";
        // 创建一个服务(service)调用(call)
        org.apache.axis.client.Service service = new org.apache.axis.client.Service();
        // 创建一个服务(service)调用(call)
        Call call = (Call) service.createCall();// 通过service创建call对象
        // 设置service所在URL
        call.setTargetEndpointAddress(endpoint);
        call.setOperationName(new QName(soapaction, method));
        //设置参数及类型,与接口参数对应
        call.addParameter(new QName(soapaction, "tag"),
                org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
        call.setUseSOAPAction(true);
        call.setReturnType(org.apache.axis.encoding.XMLType.SOAP_STRING); //返回参数的类型
        call.setSOAPActionURI(soapaction + method); //这个也要注意 就是要加上要调用的方法getStoreList,不然也会报错
        call.setProperty(org.apache.axis.MessageContext.HTTP_TRANSPORT_VERSION, HTTPConstants.HEADER_PROTOCOL_V11);

        //invoke调用方法并传递参数,获取XML
        String xmlStr = (String) call.invoke(new Object[]{"tag99"});

        return xmlStr;
    }



2、方式二

    @GetMapping("/getWeb2")
    public String getWeb3() {

        // xml传递参数
        String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\n" +
                "  <soap12:Body>\n" +
                "    <GetVal xmlns=\"http://tempuri.org/\">\n" +
                "      <tag>tag99</tag>\n" +
                "    </GetVal>\n" +
                "  </soap12:Body>\n" +
                "</soap12:Envelope>";

        // 创建HttpClient
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        // 创建Post请求
        HttpPost httpPost = new HttpPost("http://172.10.10.143:44345/MainInst.asmx");
        // 设置Header
        httpPost.setHeader("Content-Type", "text/xml; charset=UTF-8");

        // 响应模型
        CloseableHttpResponse response = null;

        try {

            StringEntity entity = new StringEntity(xml, "UTF-8");
            // post请求是将参数放在请求体里面传过去的;这里将entity放入post请求体中
            httpPost.setEntity(entity);

            // 由客户端执行(发送)Post请求
            response = httpClient.execute(httpPost);
            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();

            System.out.println("响应状态为:" + response.getStatusLine());
            if (responseEntity != null) {
                System.out.println("响应内容长度为:" + responseEntity.getContentLength());
                System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "SUCCESS";
    }



版权声明:本文为m0_46487331原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。