webService客户端传xml请求 直接main方法运行

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.dom4j.Document;
import org.dom4j.io.SAXReader;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpResponseException;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;


public class WebServiceUtils {
// public static final String WebServiceNamespace
// ="http://WebXml.com.cn/";//http://webservice.keResource.fai.sysware.com";//地址
// public static final String WebAddress =
// "http://192.168.145.110:80/sysware/services/KeResourceWS?wsdl";//地址
public static Object callWebservice(String WebServiceNamespace, String WebServiceUrl, String method,
String[] params, Object[] values) throws HttpResponseException, IOException, XmlPullParserException {
Object result = null;

SoapObject rpc = new SoapObject(WebServiceNamespace, method);
if (params != null) {
for (int i = 0; i < params.length; i++)
rpc.addProperty(params[i], values[i]);
}
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = rpc;
envelope.dotNet = true;
envelope.setOutputSoapObject(rpc);

HttpTransportSE ht = new HttpTransportSE(WebServiceUrl, 1000);
ht.debug = false;

String SOAP_ACTION = WebServiceNamespace + method;
ht.call(SOAP_ACTION, envelope);
result = envelope.getResponse();
if (result != null) {
return result;
} else {
return null;
}

}
public static void main(String[] args) {
String nameSp = "http://www.teamcenter.com/webservice";
String webUrl = "http://tshserver:8080/ws";
String method = "createBOMRequest";
String xml = getXmlString("C:\\test1.xml");
String para = "xmlString";
try {
Object resultObject = callWebservice(nameSp,webUrl,method,new String[]{para},new Object[]{xml});
SoapPrimitive result = (SoapPrimitive) resultObject;
System.out.println("result=====" + result);
} catch (IOException | XmlPullParserException e) {
e.printStackTrace();
}
}
public static String getXmlString(String path) {
String xml="";
try {
FileInputStream fis = new FileInputStream(new File(path));
SAXReader reader = new SAXReader();
Document document = reader.read(fis);
xml= document.asXML();
} catch (Exception e) {
e.printStackTrace();
}
return xml;
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值