在Lotus Designer开发工具中可以使用Java编写Webservice以及访问Webservice。这里只介绍一下如何访问Websercice。(续)
其次在Designer中创建一java脚本库,用于连接、发送Soap请求、返回结果的类:WebServiceClient 提供连接方法openWSDLURL,发送Soap请求并返回结果的方法sendSOAPRequest等
import lotus.domino.*; import javax.xml.rpc.holders.*; import java.net.*; import java.io.*; public class WebServiceClient { private URL wsURL; private URLConnection wsURLCon; private HttpURLConnection httpUrlConnection; private OutputStreamWriter outStream; //输出流:发送出去 private InputStreamReader inStream; //输入流:返回的值 private BufferedReader bufferedreader; Private String sURL,sSoapActionName; public void WebServiceClient(){ } public boolean openWSDLURL(String pURL,String pSoapActionName){ boolean bool = true; try{ sURL = pURL; sSoapActionName = pSoapActionName; wsURL = new URL(sURL); wsURLCon = wsURL.openConnection();//打开和URL之间的连接 httpUrlConnection = (HttpURLConnection)wsURLCon; }catch(java.net.MalformedURLException ue){ bool = false; ue.printStackTrace(); System.out.println(ue.toString()); }catch(java.io.IOException ioe){ bool = false; ioe.printStackTrace(); System.out.println(ioe.toString()); } return bool; } public String sendSOAPRequest(String pSendXML){ System.out.println("pSendXML:"+pSendXML); String rXML = ""; //发送POST请求,需设置使用输入(默认True)输出流;如果是Get请求,直接通过wsURLCon.connect(); 建立实际的连接 httpUrlConnection.setDoOutput(true); httpUrlConnection.setDoInput(true); // Post 请求不能使用缓存 httpUrlConnection.setUseCaches(false); //设置请求头 httpUrlConnection.setRequestProperty("Content-Type","text/xml;charset=UTF-8"); httpUrlConnection.setRequestProperty("SOAPAction",sSoapActionName); try{ httpUrlConnection.setRequestMethod("POST"); httpUrlConnection.connect(); outStream = new OutputStreamWriter(httpUrlConnection.getOutputStream(),"UTF-8"); outStream.write(pSendXML); outStream.flush(); outStream.close(); inStream =new InputStreamReader(httpUrlConnection.getInputStream(),"UTF-8"); //程序确定远程响应是字符流,采用字符流读取 bufferedreader = new BufferedReader(inStream); //此处读取行数据,如果程序无法确定远程响应是字符流,则使用字节流读取即可 for(String tmpStr = bufferedreader.readLine(); tmpStr != null; tmpStr = bufferedreader.readLine()) rXML += tmpStr; bufferedreader.close(); inStream.close(); }catch(java.io.IOException ioe){ System.out.println("IOE error:"+ ioe.toString()); ioe.printStackTrace(); }finally{ recycle(); } System.out.println("rXML:"+rXML); return rXML; } private void recycle(){ try{ if (outStream != null){ outStream.close(); } if (inStream != null){ inStream.close(); } }catch (IOException ex){ ex.printStackTrace(); } } }