webserver soap wsdl 整理运用

来源:互联网 发布:淘宝申请退款会不退吗 编辑:程序博客网 时间:2024/05/18 03:51

首先整理几个概念

web service:远程调用的一种方案。一种解决跨平台、跨语言间的分布式系统的集成(整合)方案


esb:enterprise service bus企业服务总线


soap:simple object access protocal简单对象访问协议(http + xml)


soa:service oriented acrchietecture(面向服务的架构)


wsdl:web service description language ,web service 描述语言,是一门基于 XML 的语言,用于描述 Web Services 以及如何对它们进行访问


关于webserver的基础概念这里有详细的解读,

http://blog.csdn.net/zhuizhuziwo/article/details/8153327点击打开链接 


本文主要记录一些wsdl运用soap协议在实际功能中的运用

首先拿到的wsdl文件,如下:



从文件中能获取到的信息主要是webserver地址和参数

soap的数据传输是以xml进行的,所以在wsdl的描述中,还要有xml的传输格式:

 private static String getSoapRequest(String parm) {    String qASoap = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+ "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"+ "<soapenv:Header>"+"<ns1:username xmlns:ns1=\"Authorization\">SYSADMIN</ns1:username>"+"<ns1:password xmlns:ns1=\"Authorization\">1</ns1:password>"+ "</soapenv:Header>"+ "<soapenv:Body>"+ "<ns1:mainWsEntranceControl xmlns:ns1=\"http://service.inf.xcws.zjbh.lsbh\">"+"<ns1:in0>"+parm+"</ns1:in0>"+"</ns1:mainWsEntranceControl>"+ "</soapenv:Body>"+ "</soapenv:Envelope>";return qASoap;}

参数的拼接按照wsdl中的描述即可,参数变量parm部分自行传入

<?xml version=\"1.0\" encoding=\"UTF-8\"?> xml表头可能在wsdl文档中没有,传参时记得加上,表头表示xml版本和编码等信息


下面就是主要的通信部分了,先贴上代码:

  private static InputStream getSoapInputStream() throws Exception {        try {            String soap = getSoapRequest(getParm());            if (soap == null) {                return null;            }            URL url = new URL("http://10.147.249.133:8180/bhp/pf_webservice/xcwsinf/zjBhPubWsService/service");            HttpURLConnection conn = (HttpURLConnection) url.openConnection();            conn.setUseCaches(false);            conn.setDoInput(true);            conn.setDoOutput(true);            conn.setRequestProperty("Content-Length", Integer.toString(soap.length()));            conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");            conn.setRequestMethod("POST");//            conn.addRequestProperty("SOAPAction", "http://10.147.249.133:8180/bhp/pf_webservice/xcwsinf/zjBhPubWsService/service");//            conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");//            conn.setRequestProperty("SOAPAction", "http://10.147.249.133:8180/bhp/pf_webservice/xcwsinf/zjBhPubWsService/service");//            conn.setRequestProperty("action", "http://service.inf.xcws.zjbh.lsbh/IZjBhPubWsServicePortType/mainWsEntranceControlRequest");            OutputStream os = conn.getOutputStream();           os.write(soap.getBytes());            InputStream is = conn.getInputStream();            return is;        } catch (Exception e) {            e.printStackTrace();            return null;        }    }

Java中可以使用HttpURLConnection来请求WEB资源。 

关于HttpURLConnection可以参考这篇博文,很详细  http://blog.csdn.net/woxueliuyun/article/details/43267365点击打开链接


HttpURLConnection对象不能直接构造,需要通过URL.openConnection()来获得HttpURLConnection对象,示例代码如下: 

1 String szUrl = "http://www.ee2ee.com/";
2 URL url = new URL(szUrl);
3 HttpURLConnection urlCon = (HttpURLConnection)url.openConnection();

HttpURLConnection是基于HTTP协议的,其底层通过socket通信实现。如果不设置超时(timeout),在网络异常的情况下,可能会导致程序僵死而不继续往下执行。可以通过以下两个语句来设置相应的超时: 
System.setProperty("sun.net.client.defaultConnectTimeout", 超时毫秒数字符串); 
System.setProperty("sun.net.client.defaultReadTimeout", 超时毫秒数字符串); 

例:

1HttpURLConnection urlCon= (HttpURLConnection)url.openConnection();
2urlCon.setConnectTimeout(30000);
3urlCon.setReadTimeout(30000);


基于socket的HttpURLConnection的传参就是熟悉的数据流传输:

 

   OutputStream os = conn.getOutputStream();           os.write(soap.getBytes());            InputStream is = conn.getInputStream();            return is;
HttpURLConnection对象中获取到输入输出流对象,将xml字符串参数转成byte字节写进输出流中

即可在输入流中获取到服务器返回的数据流,后面就是处理返回数据的事了。