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;
	}

}
  • TestWebServiceRequest.rar (827.6 KB)
  • 下载次数: 1

你可能感兴趣的:(xml,webservice,java)