首页 > 代码库 > 【webservice】调试方法篇(二)
【webservice】调试方法篇(二)
前面提过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
【webservice】调试方法篇(二)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。