记java调用WebService .Wsdl

1.简单webService调用

首先拿到一个wsdl链接,如http://ws.webxml.com.cn/webservices/ChinaTVprogramWebService.asmx?WSDL

对于其中方法的调用简单来说是httpPost请求+soap格式,
例如调用其中的getTVstationDataSet这个方法
可以打开其对应的方法说明页面,http://ws.webxml.com.cn/webservices/ChinaTVprogramWebService.asmx?op=getTVstationDataSet
可以在这里看到该方法的传参说明(传参格式soap1.1与soap1.2,若是找不到方法说明页面,应该是使用soap1.1进行传参)
在这里插入图片描述
通过以上,我们就可以使用httpClient发送请求,并获得响应内容
代码如下,

 /**
     * @Author Chen
     * @Description //TODO 使用httpclient请求对webService进行请求
     * @Date  2019/11/25
     * @Param [mapXml使用map用于传参, soapAction传入方法对应的soapAction, method传入方法名]
     * @return java.lang.String
     **/
    public static String doSoap1_1(Map<String, Object> mapXml,String soapAction,String method) {
        String xml = "";
        String ret;

        // HttpClient
        CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
        //这里将wsdl地址填入
        HttpPost httpPost = new HttpPost("http://ws.webxml.com.cn/webservices/ChinaTVprogramWebService.asmx?wsdl");
        //  设置请求和传输超时时间
        RequestConfig requestConfig = RequestConfig.custom()
                .setSocketTimeout( 3000 )
                .setConnectTimeout( 3000 ).build();
        httpPost.setConfig(requestConfig);
        try {
            //这里添加入请求头信息
            httpPost.setHeader("Content-Type", "text/xml; charset=utf-8" );
            httpPost.setHeader("SOAPAction", soapAction );
            //对soap信息进行拼接
            xml += "<?xml version=\"1.0\"  encoding=\"utf-8\"?>\n"+
                    "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xds=\"http://www.w3.org/2001/XMLSchema\"  xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
                    "   <soap:Body>\n" +
                    "      <" + method + " xmlns=\""+targetNameSpace+"\">\n";
            int i = 0;
            for ( String key : mapXml.keySet() ) {
//                xml += "         <arg" + i + "><![CDATA[" + mapXml.get( key ) + "]]></arg" + i + ">\n";
                xml += "         <" + key + ">" + mapXml.get( key ) + "</" + key + ">\n";
                i++;
            }
            xml += "      </" + method + ">\n" +
                    "   </soap:Body>\n" +
                    "</soap:Envelope>";
            StringEntity data = new StringEntity( xml, Charset.forName("UTF-8") );
            httpPost.setEntity(data);
            CloseableHttpResponse response = closeableHttpClient
                    .execute(httpPost);
            HttpEntity httpEntity = response.getEntity();

            if (httpEntity != null) {
                // 打印响应内容
                ret=   EntityUtils.toString(httpEntity, "UTF-8");
                closeableHttpClient.close();
            }else{
                System.out.println("无返回!请检查参数后重试!");
                ret="404";
                closeableHttpClient.close();
            }
            // 释放资源
        } catch (Exception e) {
            System.out.println("Line96:"+"exception in doPostSoap1_1" + e);
            return "404";
        }
        return ret;
    }

    public static void main(String [] args){
        //当传入多参数时,会有次序问题,注意顺序
        Map map=new LinkedHashMap();
        map.put("theAreaID",25);
        String getTVstationDataSet = doSoap1_1(map, "http://WebXml.com.cn/getTVstationDataSet", "getTVstationDataSet");
        System.out.println("返回结果"+getTVstationDataSet);
    }

调用main方法,成功拿到返回的soap数据,若请求时,需要连续请求,请在doSoap1-1中的 closeableHttpClient.close() 全部注释掉
在这里插入图片描述

2.复杂的WebService调用

当参数较多,或接受值较为复杂时,一方面需要对数据进行构造,另一方面需要对接受值进行解析

此时要使用java去生成客户端
1.使用java jdk原生工具进行生成
2.使用eclipse 的httpClient进行生成
第一种方式可以参见以下链接
简书-wsimport生成接口代码

那接下简单介绍下第二种方法,使用工具eclipse
首先说一下我的eclipse版本
在这里插入图片描述
接下来,使用eclipse创建一个project在这里插入图片描述
接下来输入项目名并直接finish(笔者使用的是jdk1.8)
在这里插入图片描述
这样在项目目录中得到了test项目在这里插入图片描述
接下来开始使用httpClient生成代码,步骤如下
在这里插入图片描述
在这里插入图片描述
最后点击next
在这里插入图片描述
此时在项目中就生成了客户端代码
在这里插入图片描述
新建一个class文件,对方法进行调用,代码如下

package cn.com.WebXml;

import java.rmi.RemoteException;

import org.apache.axis.message.MessageElement;

import sun.applet.Main;

public class test {
	public static void main(String[] args) {
		//构建客户端
		ChinaTVprogramWebServiceSoapProxy proxy = new ChinaTVprogramWebServiceSoapProxy();
		 GetAreaDataSetResponseGetAreaDataSetResult areaDataSet=null;
		try {
			//使用“getAreaDataSet方法”
			 areaDataSet = proxy.getAreaDataSet();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//输出响应数据
		MessageElement[] get_any = areaDataSet.get_any();
		for(MessageElement element:get_any){
			System.out.println("value====="+element);
		}
	}

}

调用main方法,发现报错警告: Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled.
缺少依赖包,在工程中导入需要的jar包
在这里插入图片描述
运行代码即可,返回内容如下:在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

灵湖映北辰

年轻人,要讲武德!!!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值