Domino中使用Java访问Webservice(二)

在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();
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值