JAVA调用.net 的WebService方法
2012-03-28 16:51
423 查看
看到网上很多人找寻这方面的代码。刚刚小弟前段日子也写了类似的一个代码
这个是测试代码:
SoapConnection类
这个是测试代码:
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(); } } }
相关文章推荐
- WebService java 调用 .net方法
- [z]Java调用.NET webservice方法的几种方式
- Java调用.NET webservice方法的几种方式
- Java调用.Net WebService参数为空解决办法 (远程)调试webservice方法
- .NET调用JAVA的WebService方法
- java调用.NET的webservice实例的二种方法soap方式axis模式
- .NET调用JAVA的WebService方法
- Java调用.NET webservice方法的几种方式
- .NET调用JAVA的WebService方法
- Java调用.Net WebService参数为空解决办法 (远程)调试webservice方法 转
- java使用AXIS调用.net的webservice的小说明(webservice方法里含有对象)
- Java调用.net WebService接口方法步骤
- java调用.netwebservice的方法,返回类型为XmlDocument
- Jquery 调用.net WebService 返回Json、XML方法
- .Net调用Java的WebService之亲身体验
- java调用.net生成的WebService(axis调用)
- .Net下采用GET/POST/SOAP方式动态调用WebService的简易灵活方法(C#)
- java使用axis调用webservice时出现Connection reset by peer: socket write error的一种解决方法
- java调用.net的webservice接口
- Java调用.net webservice