前面提过webservice调试工具,如果有兴趣动手写测试代码,那挺好的啊!
我也是这样想的,有想法不妨去尝试哦。那么,我的程序中主要用到HttpURLConnection类,先拼接符合soap协议的xml字符串信息,再与webservice服务端建立连接后,发送http请求,接收完返回信息后打印出来。流程上和SoapUI 调试工具的差不多的,下面提供测试程序的代码,大家可以参考一下。
import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; public class Http { public String httpGet(String httpURL){ String reqMsg = "<soapenv:Envelope xmlns:soapenv=\"http://www.w3.org/2003/05/soap-envelope\">" + "<soapenv:Header>" + "<ns1:Username xmlns:ns1=\"http://service.webservice.ultrapower.com.cn\" soapenv:mustUnderstand=\"false\">username</ns1:Username>" + "<ns1:Password xmlns:ns1=\"http://service.webservice.ultrapower.com.cn\" soapenv:mustUnderstand=\"false\">userpw</ns1:Password>" + "</soapenv:Header>" + "<soapenv:Body>" + "<ns1:SendXML xmlns:ns1=\"http://service.webservice.ultrapower.com.cn\">" + "<ns1:requestXml>" + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<ReadWhiteList>" + "<srcDeviceType>32</srcDeviceType>" + "<srcdeviceid>321101</srcdeviceid>" + "<Cmd>0</Cmd>" + "</ReadWhiteList>" + "</ns1:requestXml>" + "<ns1:busiKey>11</ns1:busiKey>" + "</ns1:SendXML>" + "</soapenv:Body>" + "</soapenv:Envelope>"; URL url = null; HttpURLConnection httpURLConn= null; StringBuffer dataString = new StringBuffer(""); String temp = new String(); try{ System.out.println(httpURL); url = new URL(httpURL); httpURLConn= (HttpURLConnection)url.openConnection(); httpURLConn.setDoOutput(true); httpURLConn.setRequestMethod("POST"); httpURLConn.setConnectTimeout(300*1000);//300s超时 httpURLConn.setRequestProperty("User-Agent","Apache-HttpClient/4.1.1 (java 1.5)"); httpURLConn.setRequestProperty("Content-Type","application/soap+xml;charset=UTF-8;action=\"urn:SendXML\""); OutputStream out = httpURLConn.getoutputStream(); out.write(reqMsg.toString().getBytes()); httpURLConn.connect(); InputStream in =httpURLConn.getInputStream(); BufferedReader bd = new BufferedReader(new InputStreamReader(in)); while((temp=bd.readLine())!=null) { dataString.append(new String(temp.getBytes())); } in.close(); bd.close(); } catch (Exception e){ e.printstacktrace(); } finally{ if(httpURLConn!=null){ httpURLConn.disconnect(); } } try{ System.out.println(new String(dataString.toString().getBytes("gbk"),"utf-8")); }catch(Exception e){ e.printstacktrace(); } return dataString.toString(); } public static void main(String[] arg){ Http pm = new Http(); pm.httpGet("http://132.xx.xxx.xx:8080/xxx//xxx?wsdl"); } }
有没有觉得拼接的这个xml,有点偏复杂?这个webservice服务端弄了soaphead 安全机制,没办法,客户端要求这样弄。往后再介绍一下这个带soaphead 安全机制的webservice服务端吧。
转载请说明出自whilejolly:http://blog.csdn.net/seedingly/article/details/39054609
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。