webService客户端传xml请求 直接main方法运行_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > webService客户端传xml请求 直接main方法运行

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

 2018/6/26 15:20:46  hufeng  程序员俱乐部  我要评论(0)
  • 摘要:importjava.io.File;importjava.io.FileInputStream;importjava.io.IOException;importorg.dom4j.Document;importorg.dom4j.io.SAXReader;importorg.ksoap2.SoapEnvelope;importorg.ksoap2.serialization.SoapObject;importorg.ksoap2.serialization.SoapPrimitive
  • 标签:方法 Web Service Webservice 运行 客户 客户端 XML
class="java">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)
  • 下载次数: 0
发表评论
用户名: 匿名