代码_AXIS:调用webservice接口发送soap报文

本文介绍了一种通过使用StringBuffer拼接SOAP报文的方法,详细讲解了如何在JAVA中调用WebService接口并发送SOAP请求的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、拼接报文的方式:

    (1)利用StringBuffer来拼接字符串,代码如下:

                StringBuffer soapData = new StringBuffer();
		soapData.append("<?xml version=\"1.0\"?>");
		soapData.append("<ImageSearch provider=\"ATM-WF\" orgid=\"99\" branchid=\"99\" businessorgid=\"99\" busine                                    ssbranchid=\"ATM99\" userid=\"");
		soapData.append(userId);		
		soapData.append("\" type=\"FR_BaseView\">");
		soapData.append("<InitSoapAddress>");
		soapData.append("http://10.112.**.*:8004/ImageViewPortal/services/ImageViewService?wsdl");
		soapData.append("</InitSoapAddress>");
		soapData.append("<InitSoapService>ImageViewService</InitSoapService>");
		soapData.append("<InitSoapAction>execute</InitSoapAction>");
		soapData.append("<Input>");
		soapData.append("<QueryTerm1>");
		soapData.append(signImageIndex);
		soapData.append("</QueryTerm1>");
		soapData.append("</Input>");
		soapData.append("</ImageSearch>");
		return soapData.toString();
    (2)利用dom4j拼接xml文件,代码如下:
public static void main(String args[]) {
		Document document = DocumentHelper.createDocument();	//声明document
		Element root = DocumentHelper.createElement("ImageSearch");		//根节点		
		document.setRootElement(root);	//将根节点放入document
		root.addAttribute("provider","ATM-WF");	//添加属性
		root.addAttribute("branchId ", "99");
		Element InitSoapAddress = root.addElement("InitSoapAddress");	//子节点
		InitSoapAddress.addText(" http://10.112.*.***:8***/ImageViewPortal/services/ImageViewService?wsdl");	//添加内容
		Element InitSoapService = root.addElement("InitSoapService");
		InitSoapService.setText("ImageViewService");
		Element InitSoapAction = root.addElement("InitSoapAction");
		InitSoapAction.addText("execute");	
		XMLWriter str = new XMLWriter();	
		try {
			str.write(document);	//控制台输出
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

二、利用AXIS的call方法发送报文,代码如下:
	public static String serUrl = "http://10.112.**.*:80**/ImageViewPortal/services/ImageViewService?wsdl";//访问接口地址
	public static String nameSpace = "http://10.112.**.*:80**/ImageViewPortal/services/ImageViewService";//wsdl文件中的namespace

		String response = null;
	        String parameter =getBASE64(xmlSoap);		//对报文进行BASE64转码,可不必
		try {
			Service service = new Service();
			Call call = (Call)service.createCall();
			QName qn = new QName(nameSpace,"execute");
			call.setTargetEndpointAddress(new java.net.URL(serUrl));
			call.setOperationName(qn);
			call.setEncodingStyle("UTF-8");
			call.addParameter("xml", XMLType.XSD_STRING,ParameterMode.IN);//xml为execute方法的参数名,后面定义类型,和方式
			call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//返回报文类型
			response = (String)call.invoke(new Object[]{parameter});
		} catch(ServiceException e) {
			e.printStackTrace();
			logger.error(e);
		} catch (RemoteException e) {
			e.printStackTrace(); 
			logger.error(e);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
三、返回报文取某个节点里的值,这里取url(返回报文格式为:
<TIV version="1.0.0"><ImageUrls><ImageUrlsrc="http://10.112.**.*:8***/ImageStoreServer//servlet/FileDownLoad?name=286456&amp;businessCode=ImageATM"><Range>1-1</Range></ImageUrl></ImageUrls></TIV>):
		Document document = null;
		try {
			document = DocumentHelper.parseText(releaseCode);
		} catch (DocumentException e) {
			logger.error("ParseTextException");
		}
		Element root = document.getRootElement();//获取根节点
		Element child = root.element("ImageUrls");//获取子节点
		Element grandson = child.element("ImageUrl");//获取子目标节点
		String url= grandson.attributeValue("src");//获取目标属性
		System.out.println(url); 
		return url;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值