JAVA调用.net 的WebService方法

看到网上很多人找寻这方面的代码。刚刚小弟前段日子也写了类似的一个代码

这个是测试代码:

public void GetAllDepts(){
		//WebServerice 地址			
		String SOAPUrl = "http://10.137.102.234:8083/WebService.asmx";
		//需要发送的XML
    	String xmlFile2Send ="config/GetAllEmployees_Send.xml";
    	//SOAPAction 一般在发布的SOAP WebService中都有明确的注明
		String SOAPAction = "http://tempuri.org/BPMService_GetAllDepts";
		//对XML的处理封装
		SendMap sendMap;
		try {
			 sendMap = new SoapCreatXml(xmlFile2Send);
			 //创建Soap的连接,并返回WebService的XML
			 SoapConnection con = new SoapConnection(sendMap.getDocument(), SOAPUrl, SOAPAction);
			 InputStream returnXML = con.getReturnXML();
			 InputStreamReader reader = new InputStreamReader(returnXML);
			 //解析XML
			 ParseMap map = new SoapXmlImpl(returnXML);
		     System.out.println(map.getNodeText("BPMService_GetAllDeptsResult", 0));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}


SoapConnection类

package soap.java.buiness;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;

public class SoapConnection {
	private InputStream returnXML;

	public InputStream getReturnXML() {
		return returnXML;
	}

	private SoapConnection() {
	};

	public SoapConnection(Document inDoc, String SOAPUrl, String SOAPAction)
			throws Exception {
		
		URL url = new URL(SOAPUrl);
		URLConnection connection = url.openConnection();
		HttpURLConnection httpConn = (HttpURLConnection) connection;
		ByteArrayOutputStream bout = new ByteArrayOutputStream();
		//写入XML
		callWriteXmlFile(inDoc, bout, "UTF-8");
		byte[] b = bout.toByteArray();
		// 设置HTTP参数
		httpConn.setRequestProperty("Content-Length", String.valueOf(b.length));
		httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
		httpConn.setRequestProperty("SOAPAction", SOAPAction);
		httpConn.setRequestMethod("POST");
		httpConn.setDoOutput(true);
		httpConn.setDoInput(true);
		//读取HTTP返回的字节流
		OutputStream out = httpConn.getOutputStream();
		out.write(b);
		out.close();
		returnXML = httpConn.getInputStream();

	}

	private void callWriteXmlFile(Document doc, OutputStream outputStream,
			String encoding) {

		try {

			Source source = new DOMSource(doc);
			StreamResult result = new StreamResult(outputStream);
			Transformer xformer = TransformerFactory.newInstance()
					.newTransformer();
			xformer.setOutputProperty(OutputKeys.ENCODING, encoding);
			xformer.transform(source, result);
		} catch (TransformerConfigurationException e) {
			e.printStackTrace();
		} catch (TransformerException e) {
			e.printStackTrace();
		}
	}
	
}

 

你可能感兴趣的:(JAVA调用.net 的WebService方法)