用Java来输出soap报文

前段时间开发acs,是基于soap协议的通信,一次通信过程包含多个soap报文,而且也不想普通的webserivice那样,

 soap报文是自动生成的。acs的通信的报文是硬编码编出来的,虽然能正确运行,但是实在是不雅,最近闲来无事,

 想着如何用像webservice那样,用java对象设置参数后,将对象转换成string格式的soap报文,这样以后程序的维护

 问题就得到解决了。

  然后在google中baidu了一把,找了个例子,然后开始试验了。其实程序并不难,写这个文章,主要是帮助那些直接使用soap

协议进行开发的朋友们参考,也给自己留个备注,呵呵。

package com.seahigh.acs.soap;

import java.util.arraylist;
import java.util.list;

import javax.xml.namespace.qname;
import javax.xml.parsers.documentbuilderfactory;
import javax.xml.soap.messagefactory;
import javax.xml.soap.soapbody;
import javax.xml.soap.soapelement;
import javax.xml.soap.soapenvelope;
import javax.xml.soap.soapexception;
import javax.xml.soap.soapfactory;
import javax.xml.soap.soapheader;
import javax.xml.soap.soapmessage;
import javax.xml.soap.soappart;
import javax.xml.transform.outputkeys;
import javax.xml.transform.source;
import javax.xml.transform.transformer;
import javax.xml.transform.transformerfactory;
import javax.xml.transform.dom.domsource;
import javax.xml.transform.sax.saxsource;
import javax.xml.transform.stream.streamresult;

import org.w3c.dom.document;
import org.w3c.dom.node;
import org.xml.sax.inputsource;
/**
 * 
 * @author 汪心利
 * date 2010-3-30下午02:51:30
 * (c)copyright seahigh 2010
 */
public class soaputil {
	public soappart initsoappart() throws soapexception {

		soapmessage soapmessage = messagefactory.newinstance().createmessage();

		soappart soappart = soapmessage.getsoappart();

		soapenvelope soapenvelope = soappart.getenvelope();
		soapheader soapheader = soapenvelope.getheader();
		soapelement cwmp = soapenvelope.addnamespacedeclaration("cwmp",
				"urn:dslforum-org:cwmp-1-0");
		soapelement xsi = soapenvelope.addnamespacedeclaration("xsi",
				"http://www.w3.org/2001/xmlschema-instance");
		soapelement xsd = soapenvelope.addnamespacedeclaration("xsd",
				"http://www.w3.org/2001/xmlschema");

		soapelement enc = soapenvelope.addnamespacedeclaration("soap-enc",
				"http://schemas.xmlsoap.org/soap/encoding/");

		soapelement id = soapheader.addchildelement("id", "cwmp");
		id.settextcontent("1");
		return soappart;
	}

	public void soap2string(source source) throws exception {
		if (source != null) {
			node root = null;
			if (source instanceof domsource) {
				root = ((domsource) source).getnode();
			} else if (source instanceof saxsource) {
				inputsource insource = ((saxsource) source).getinputsource();
				documentbuilderfactory dbf = documentbuilderfactory
						.newinstance();
				dbf.setnamespaceaware(true);
				document doc = dbf.newdocumentbuilder().parse(insource);
				root = (node) doc.getdocumentelement();
			}
			transformer transformer = transformerfactory.newinstance()
					.newtransformer();
			transformer.setoutputproperty(outputkeys.indent, "yes");
			transformer.transform(new domsource(root), new streamresult(
					system.out));
		}
	}

	public source informresponse(soappart part) throws exception {
		soapenvelope soapenvelope = part.getenvelope();
		soapbody soapbody = soapenvelope.getbody();
		soapelement informres = soapbody.addchildelement("informresponse",
				"cwmp");
		soapelement max = soapfactory.newinstance().createelement(
				"maxenvelopes", "", "");
		max.settextcontent("1");
		informres.addchildelement(max);
		return part.getcontent();
	}

	public source getparametervalues(soappart part, list list) throws exception {
		soapenvelope soapenvelope = part.getenvelope();
		soapbody soapbody = soapenvelope.getbody();
		soapelement informres = soapbody.addchildelement("getparametervalues",
				"cwmp");
		soapfactory soapfactory = soapfactory.newinstance();
		soapelement names = soapfactory.createelement("parameternames", "", "");
		names.addattribute(new qname("soap-enc:arraytype"), "xsd:string["
				+ list.size() + "]");
		soapelement nameelement = null;
		for (int i = 0; i < list.size(); i++) {
			nameelement = soapfactory.createelement("string", "", "");
			nameelement.settextcontent((string) list.get(i));
			names.addchildelement(nameelement);
		}
		informres.addchildelement(names);
		return part.getcontent();
	}

	public static void main(string[] args) throws exception {
		soaputil util = new soaputil();
		soappart part = util.initsoappart();
		source inform = util.getparametervalues(part, util.gettestnames());
		util.soap2string(inform);
	}

	public list<string> gettestnames() {
		list<string> list = new arraylist<string>();
		list.add("internetgatewaydevice.deviceinfo.x_ct-com_cpu");
		list.add("internetgatewaydevice.deviceinfo.x_ct-com_worktime");
		list.add("internetgatewaydevice.deviceinfo.softwareversion");
		list.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.ssid");
		list
				.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_receivenoise");
		list
				.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_lan-totalbytesreceived");
		list
				.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_lan-totalbytessent");
		list
				.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_wan-totalbytesreceived");
		list
				.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_wan-totalbytessent");
		list
				.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_lan-totalpacketsreceived");
		list
				.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_lan-totalpacketssent");
		list
				.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_wan-totalpacketsreceived");
		list
				.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_wan-totalpacketssent");
		list
				.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.responsepass");
		list
				.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.askpass");
		list
				.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.successpass");
		list.add("internetgatewaydevice.deviceinfo.x_ct-com_temp");
		list
				.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.lan-packetserror");
		list
				.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.lan-totalbytesreceived");
		list
				.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.lan-totalbytessent");
		list
				.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.wan-totalbytesreceived");
		list
				.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.wan-packetserror");
		list
				.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.wan-totalbytessent");
		list
				.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.associateddevice.1.x_ct-com_receiverate");
		list
				.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.associateddevice.1.x_ct-com_sendrate");
		list.add("internetgatewaydevice.deviceinfo.serialnumber");
		list.add("internetgatewaydevice.deviceinfo.manufactureroui");
		return list;
	}
}

 

这就是研究的成果了,看下结果:

对应tr069协议中的rcp method:getparametervalues

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<soap-env:envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap-enc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:cwmp="urn:dslforum-org:cwmp-1-0" xmlns:xsd="http://www.w3.org/2001/xmlschema" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance">
<soap-env:header>
<cwmp:id>1</cwmp:id>
</soap-env:header>
<soap-env:body>
<cwmp:getparametervalues>
<parameternames soap-enc:arraytype="xsd:string[27]">
<string>internetgatewaydevice.deviceinfo.x_ct-com_cpu</string>
<string>internetgatewaydevice.deviceinfo.x_ct-com_worktime</string>
<string>internetgatewaydevice.deviceinfo.softwareversion</string>
<string>internetgatewaydevice.landevice.1.wlanconfiguration.1.ssid</string>
<string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_receivenoise</string>
<string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_lan-totalbytesreceived</string>
<string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_lan-totalbytessent</string>
<string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_wan-totalbytesreceived</string>
<string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_wan-totalbytessent</string>
<string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_lan-totalpacketsreceived</string>
<string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_lan-totalpacketssent</string>
<string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_wan-totalpacketsreceived</string>
<string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_wan-totalpacketssent</string>
<string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.responsepass</string>
<string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.askpass</string>
<string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.successpass</string>
<string>internetgatewaydevice.deviceinfo.x_ct-com_temp</string>
<string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.lan-packetserror</string>
<string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.lan-totalbytesreceived</string>
<string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.lan-totalbytessent</string>
<string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.wan-totalbytesreceived</string>
<string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.wan-packetserror</string>
<string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.wan-totalbytessent</string>
<string>internetgatewaydevice.landevice.1.wlanconfiguration.1.associateddevice.1.x_ct-com_receiverate</string>
<string>internetgatewaydevice.landevice.1.wlanconfiguration.1.associateddevice.1.x_ct-com_sendrate</string>
<string>internetgatewaydevice.deviceinfo.serialnumber</string>
<string>internetgatewaydevice.deviceinfo.manufactureroui</string>
</parameternames>
</cwmp:getparametervalues>
</soap-env:body>
</soap-env:envelope>

 cpe informresponse

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<soap-env:envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap-enc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:cwmp="urn:dslforum-org:cwmp-1-0" xmlns:xsd="http://www.w3.org/2001/xmlschema" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance">
<soap-env:header>
<cwmp:id>1</cwmp:id>
</soap-env:header>
<soap-env:body>
<cwmp:informresponse>
<maxenvelopes>1</maxenvelopes>
</cwmp:informresponse>
</soap-env:body>
</soap-env:envelope>
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Java中发送SOAP报文,可以使用SOAP协议和相关的Java库来实现。 首先,你需要使用Java的Web服务相关的库来创建一个SOAP客户端。最常用的库是JAX-WS(Java API for XML Web Services)和Apache Axis。这些库提供了方便的API,用于创建和发送SOAP报文。 在使用这些库之前,你需要先定义一个WSDL(Web Services Description Language)文件,其中描述了你的SOAP服务的接口和操作。你可以使用WSDL编辑器或者手动编写WSDL文件。然后,利用JAX-WS提供的工具来生成Java类,这些类将用于创建和解析SOAP报文。 接下来,你需要创建一个SOAP客户端。使用生成的Java类,你可以通过调用相应的方法来构建SOAP请求报文。然后,将构建的SOAP请求报文发送到SOAP服务的URL。这可以通过在Java程序中使用HTTP连接来实现。你可以使用Java提供的URLConnection或Apache HttpClient等库来发送HTTP请求,并将SOAP请求作为HTTP请求的正文发送。 最后,你需要处理SOAP服务返回的SOAP响应报文。通过解析SOAP响应报文,你可以提取其中的数据并进行相应的处理。 总结起来,实现Java发送SOAP报文的步骤包括: 1. 定义WSDL文件描述SOAP服务接口和操作。 2. 使用JAX-WS生成Java类。 3. 创建SOAP客户端,构建SOAP请求报文,并发送到SOAP服务的URL。 4. 处理SOAP服务的响应报文,提取数据并进行相应的处理。 以上是一个简单的概述,实际的实现可能会涉及更多的细节和配置。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值