webservice接口的三种调用方式

还是那句话,如果不是工作上要用都2022年了,谁还会用这种方式去调用接口,无奈还是特此记录一下吧 axis调用参考这位博主https://blog.csdn.net/qq_33236248/article/details/80436688

一 相关依赖

 <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>2.0.0</version>
        </dependency>

        <!-- axis -->
        <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>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.axis</groupId>
            <artifactId>axis-jaxrpc</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis</groupId>
            <artifactId>axis-saaj</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>wsdl4j</groupId>
            <artifactId>wsdl4j</artifactId>
            <version>1.4</version>
        </dependency>
        <!-- mail -->
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.7</version>
        </dependency>

二 代码demo

package com.ayx.demo;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.http.HttpEntity;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;

import javax.xml.namespace.QName;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

public class WebServiceDemo {

    //httpClient方式
    public static void test2(String[] args) {
        //2022年2月21日21:18:29  这里只联系webservice接口的调用和xml解析  太特么老的东西了,都特么2022年了


        String reqData = "<?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" +
                "    <getSupportCityDataset xmlns=\"http://WebXml.com.cn/\">\n" +
                "      <theRegionCode>31110</theRegionCode>\n" +
                "    </getSupportCityDataset>\n" +
                "  </soap12:Body>\n" +
                "</soap12:Envelope>";


        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();

        //构建出httpClient客户端
        CloseableHttpClient httpClient = httpClientBuilder.build();

        //构建请求体
        HttpPost httpPost = new HttpPost("http://ws.webxml.com.cn/WebServices/WeatherWS.asmx");
        //设置请求头
        httpPost.setHeader("Content-Type", "text/xml;charset=utf-8");

        StringEntity stringEntity = new StringEntity(reqData, StandardCharsets.UTF_8);
        httpPost.setEntity(stringEntity);

        //发送请求
        try {
            CloseableHttpResponse response = httpClient.execute(httpPost);
            System.out.println(response);
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            StringBuilder sb = new StringBuilder();
            byte[] bytes = new byte[8024];
            int len = -1;
            while ((len = content.read(bytes)) != -1) {
                String s = new String(bytes, 0, len);
                sb.append(s);
            }
            System.out.println(sb.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    //httpURLConnection方式
    public static void main2(String[] args) {
        String url = "http://ws.webxml.com.cn/WebServices/WeatherWS.asmx";
        String reqData = "<?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" +
                "    <getSupportCityDataset xmlns=\"http://WebXml.com.cn/\">\n" +
                "      <theRegionCode>31110</theRegionCode>\n" +
                "    </getSupportCityDataset>\n" +
                "  </soap12:Body>\n" +
                "</soap12:Envelope>";
        try {
            URL url1 = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) url1.openConnection();
            //设置参数
            connection.setRequestMethod("GET");
            connection.setRequestProperty("Content-type","text/xml;charset=utf-8");
//            设置输入输出,因为默认新创建的connection没有读写权限,
            connection.setDoInput(true);
            connection.setDoOutput(true);
            //以流的方式将参数发送出去
            OutputStream outputStream = connection.getOutputStream();
            outputStream.write(reqData.getBytes(StandardCharsets.UTF_8));
            //接收返回数据
            InputStream inputStream = connection.getInputStream();
            byte[] bytes = new byte[8024];
            int len = -1;
            StringBuilder res = new StringBuilder();
           if (connection.getResponseCode()==200){
               while ((len = inputStream.read(bytes)) != -1) {
                   res.append(new String(bytes, 0, len));
               }
               System.out.println(res);
           }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //axis方式
    public static void main(String[] args) {
        try {
            //字符集
            String encodingStyle = "utf-8";
            //WSDL的地址
            String endpoint = "http://www.webxml.com.cn/WebServices/RandomFontsWebService.asmx?wsdl";
            //命名空间,在WSDL中对应的标签是:参见说明第3条
            String targetNamespace = "http://WebXml.com.cn/";
            //具体方法的调用URI,在WSDL中对应的标签是:参见说明第4条
            String soapActionURI = "http://WebXml.com.cn/getCharFonts";
            //具体调用的方法名,在WSDL中对应的标签是:参见说明第5条
            String method = "getCharFonts";
            //调用接口的参数的名字
            String[] paramNames = {"byFontsLength"};
            //调用接口的参数的值
            Integer[] paramValues = {1};

            Service service = new Service();
            Call call = (Call) service.createCall();
//            call.setTimeout(new Integer(20000));  //设置超时时间
            call.setSOAPActionURI(soapActionURI);
            call.setTargetEndpointAddress(new java.net.URL(endpoint));  //设置目标接口的地址
            call.setEncodingStyle(encodingStyle);//设置传入服务端的字符集格式如utf-8等
            call.setOperationName(new QName(targetNamespace,method));// 具体调用的方法名,可以由接口提供方告诉你,也可以自己从WSDL中找
            call.setUseSOAPAction(true);
            call.addParameter(new QName(targetNamespace,paramNames[0]),
                    org.apache.axis.encoding.XMLType.XSD_INTEGER,
                    javax.xml.rpc.ParameterMode.IN);// 接口的参数
//            call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);// 设置返回类型  ,如String
            call.setReturnClass(java.lang.String[].class); //返回字符串数组类型
            // 给方法传递参数,并且调用方法 ,如果无参,则new Obe
            String[] result = (String[]) call.invoke(new Object[] {paramValues[0]});
            // 打印返回值
            System.out.println("result is " + Arrays.toString(result));
            if (result != null && result.length > 0) {
                for (int i = 0; i < result.length; i++) {
                    System.out.println(result[i]);
                }
            }
        } catch (Exception e) {
            System.err.println(e.toString());
        }
    }
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值