java调用SQL Server Reporting Services

最近研究了一下SQL Reporting Services,  在这mark一下。

 

Java调用SSRS,其实是调用了SSRS提供的一些webservice。

WSDL:

http://localhost:18080/ReportServer/ReportExecution2005.asmx

 

首先下载axis2,

运行命令wsdl2java -uri ReportExecution2005.asmx.wsdl 生成java客户端代码。

 

接着要实现ntlm验证

 

private static void setupCredential() {
		  final NTCredentials nt = new NTCredentials("user", "password", "host", "domain");
		  final CredentialsProvider myCredentialsProvider = new CredentialsProvider() {
		   public Credentials getCredentials(AuthScheme scheme, String host, int port, boolean proxy) throws CredentialsNotAvailableException {
		    return nt;
		   }
		  };
		  DefaultHttpParams.getDefaultParams().setParameter("http.authentication.credential-provider", myCredentialsProvider);
		 }

 注意,这边不能直接用axis2生成的代码,打开ReportExecutionServiceStub.java,修改soap version

 

//Set the soap version
            _serviceClient.getOptions().setSoapVersionURI(org.apache.axiom.soap.SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);

  下面是完整的测试代码

 

import java.io.FileOutputStream;
import java.io.InputStream;

import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.NTCredentials;
import org.apache.commons.httpclient.auth.AuthScheme;
import org.apache.commons.httpclient.auth.CredentialsNotAvailableException;
import org.apache.commons.httpclient.auth.CredentialsProvider;
import org.apache.commons.httpclient.params.DefaultHttpParams;

import com.microsoft.schemas.sqlserver._2005._06._30.reporting.reportingservices.ReportExecutionServiceStub;


public class Test {
	public static void main(String[] args) throws Exception {
		setupCredential();
		ReportExecutionServiceStub es=new ReportExecutionServiceStub("http://localhost:18080/ReportServer/ReportExecution2005.asmx");
		ReportExecutionServiceStub.LoadReport lr= new ReportExecutionServiceStub.LoadReport();
		lr.setReport("/Report Project1/Test Report");
		
		ReportExecutionServiceStub.LoadReportResponse info=es.loadReport(lr, null);
		
		ReportExecutionServiceStub.SetExecutionParameters param=new ReportExecutionServiceStub.SetExecutionParameters();
		ReportExecutionServiceStub.ArrayOfParameterValue ps =new ReportExecutionServiceStub.ArrayOfParameterValue();
		ReportExecutionServiceStub.ParameterValue pv=new ReportExecutionServiceStub.ParameterValue();
		pv.setValue("8888");
		pv.setName("id");
		ps.addParameterValue(pv);
		param.setParameters(ps);
		ReportExecutionServiceStub.ExecutionHeaderE eh=new ReportExecutionServiceStub.ExecutionHeaderE();
		ReportExecutionServiceStub.ExecutionHeader header=new ReportExecutionServiceStub.ExecutionHeader();
		
		header.setExecutionID(info.getExecutionInfo().getExecutionID());
		eh.setExecutionHeader(header);
		es.setExecutionParameters(param, eh, null);
		
		
		ReportExecutionServiceStub.Render render=new ReportExecutionServiceStub.Render();
		render.setFormat("PDF");
		ReportExecutionServiceStub.RenderResponse res=es.render(render, eh, null);
		InputStream in=res.getResult().getInputStream();
		
		FileOutputStream out=new FileOutputStream("Test Report.pdf");
		byte [] buff=new byte[1024];
		int lenth=-1;
		while ( ( lenth=in.read(buff))!=-1) {
			out.write(buff,0,lenth);
			
		}
		out.close();

	}
	private static void setupCredential() {
		  final NTCredentials nt = new NTCredentials("user", "password", "host", "domain");
		  final CredentialsProvider myCredentialsProvider = new CredentialsProvider() {
		   public Credentials getCredentials(AuthScheme scheme, String host, int port, boolean proxy) throws CredentialsNotAvailableException {
		    return nt;
		   }
		  };
		  DefaultHttpParams.getDefaultParams().setParameter("http.authentication.credential-provider", myCredentialsProvider);
		 }
}
 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值