import java.io.DataOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Properties;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
/**
* 发送soap工具类
*
* @author wpj_service
*
*/
public class soapClient {
private static final String url = "127.0.0.1:80";
/**
*
* @return
* @author wpj
* @throws axisFault
*/
public static String sendSoap() {
String result = "";
try {
// ---------------------头-----------------------------------------------
OMFactory factory = OMAbstractFactory.getOMFactory();
OMNamespace SecurityElementNamespace = factory.createOMNamespace(
"http://www.baidu.com.cn/common/header/in", "in");
OMElement soapHeader = factory.createOMElement("soapHeader",
SecurityElementNamespace);
OMNamespace SecurityElementNamespace1 = factory.createOMNamespace(
"http://www.w3.org/2001/XMLSchema-instance", "xsi");
soapHeader.declareNamespace(SecurityElementNamespace1);
OMElement head1 = factory.createOMElement("msgRef",
SecurityElementNamespace);
head1.setText("head1");
soapHeader.addChild(head1);
// ---------------------头end----------------------------------------------
// ---------------------体-------------------------------------------------
OMNamespace omNsBody = factory.createOMNamespace(
"http://schemas.xmlsoap.org/soap/envelope/", "soap-env");
OMElement soapBody = factory.createOMElement("Body", omNsBody);
OMNamespace omNsTargetNs = factory.createOMNamespace(
"http://www.baidu.com.cn/card",
"targetNs");
OMNamespace omNsHeader2 = factory.createOMNamespace(
"http://schemas.xmlsoap.org/soap/envelope/", "soap");
soapBody.declareNamespace(omNsTargetNs);
soapBody.declareNamespace(omNsHeader2);
soapBody.declareNamespace(omNsBody);
OMElement soapTargetNs = factory.createOMElement(
"doProcessRequest", omNsTargetNs);
OMElement body1 = factory.createOMElement("serviceCode",
omNsTargetNs);
body1.setText("body1");
//创建一个两层的body
OMElement body2 = factory.createOMElement(
"transactionDetails", omNsTargetNs);
OMElement body21 = factory
.createOMElement("phoneNo", omNsTargetNs);
body21.setText("body21");
body2.addChild(body21);
soapTargetNs.addChild(body1);
soapTargetNs.addChild(body2);
// ---------------------体end------------------------
Options options = new Options();
EndpointReference targetEPR = new EndpointReference(url);
options.setTo(targetEPR);
options.setAction("targetNs:doProcessRequest");
ServiceClient sender = new ServiceClient();
sender.setOptions(options);
sender.addHeader(soapHeader);
OMElement reOME = sender.sendReceive(soapTargetNs);
result = reOME.toString();
} catch (AxisFault axisFault) {
axisFault.printStackTrace();
}
return result;
}
使用Axis2生成soap客户端
最新推荐文章于 2024-03-29 14:06:56 发布