`

axis2调用带Authentication认证的webservice

阅读更多
package com.wujianjun.axis2.client;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.transport.http.HTTPConstants;
import org.apache.axis2.transport.http.HttpTransportProperties;

public class TestClient {
	public static void main(String[] args) {
		try {
			ServiceClient sc = new ServiceClient();
			Options opts = sc.getOptions();
			opts.setTo(new EndpointReference("http://10.10.0.68:8000/sap/bc/srt/rfc/sap/ztest/110/ztest/ztest"));
			opts.setAction("urn:ZTEST");
			HttpTransportProperties.Authenticator basicAuth = new HttpTransportProperties.Authenticator();
			basicAuth.setUsername("liuhand");
			basicAuth.setPassword("dev110");
//			basicAuthentication.setHost("xx.xx.x.xx");    
//			basicAuthentication.setDomain("xx.xx.xx.xx");  
			opts.setProperty(HTTPConstants.AUTHENTICATE, basicAuth);
			OMElement res = sc.sendReceive(createPayLoad());
			System.out.println(res);
		} catch (AxisFault e) {
			e.printStackTrace();
		}

	}

	public static OMElement createPayLoad() {
		OMFactory fac = OMAbstractFactory.getOMFactory();
		OMNamespace omNs = fac.createOMNamespace("urn:sap-com:document:sap:rfc:functions","m");
		OMElement method = fac.createOMElement("ZTEST", omNs);
		OMElement value = fac.createOMElement("IM_P", null);
		value.setText("Hello");
		method.addChild(value);
		System.out.println(method);
		return method;
	}

}
 
分享到:
评论
10 楼 香蕉8822 2012-12-23  
dabaosse 写道
你好,我调用SAP的服务时,报下面的错,好像跟解析报文有关吧? 帮忙看一下 谢谢:)

org.apache.axis2.AxisFault: com.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character 'P' (code 80) in DOCTYPE declaration; expected a space between PUBLIC keyword and public id
at [row,col {unknown-source}]: [1,22]
at org.apache.axis2.AxisFault.makeFault(AxisFault.java:430)
at org.apache.axis2.transport.TransportUtils.createSOAPMessage(TransportUtils.java:62)
at org.apache.axis2.description.OutInAxisOperationClient.handleResponse(OutInAxisOperation.java:353)
at org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:416)
at org.apache.axis2.description.OutInAxisOperationClient.executeImpl(OutInAxisOperation.java:228)
at org.apache.axis2.client.OperationClient.execute(OperationClient.java:163)
at mc_style.functions.soap.sap.document.sap_com.ZHR_RFC_PERNR_HIREServiceStub.ZhrRfcPernrHire(ZHR_RFC_PERNR_HIREServiceStub.java:158)
at mc_style.functions.soap.sap.document.sap_com.ZHR_RFC_PERNR_HIRETestCase.stub(ZHR_RFC_PERNR_HIRETestCase.java:87)
at mc_style.functions.soap.sap.document.sap_com.ZHR_RFC_PERNR_HIRETestCase.main(ZHR_RFC_PERNR_HIRETestCase.java:21)

我也遇到这个问题了呢,请问你最后解决了吗?
9 楼 wujianjun 2010-12-01  
用axis2的地址示例来说明:
Endpoint的地址:http://localhost:8080/test/services/test
WSDL的地址:http://localhost:8080/test/services/test?wsdl
调用的时候是一定要用endpoint地址的,wsdl地址只是供调用者分析查看针对Web服务的描述(有哪些方法供调用、每个方法内有哪些参数,返回值是什么)等等
8 楼 conworld 2010-11-21  
你好,
EndpointReference("http://10.10.0.68:8000/sap/bc/srt/rfc/sap/ztest/110/ztest/ztest")
这个URL好象不是Endpoint的地址,只是WSDL的URL吧
我不知道这两个地址有什么区别,但SOAMANAGER说必须用Endpoint的URL,象这样的
http://eccides:5001/sap/bc/srt/wsdl/bndg_DFE183GE39MD8DF1AD3200237DD2B8F6/wsdl11/allinone/ws_policy/document?sap-client=200
这个WSDL文件里面还有一个SOAP address
我这边的Java开发人员说不能用上面的地址,只能用WSDL文件里面的SOAP address,不知道楼主是不是也用的这个地址
7 楼 wujianjun 2010-11-12  
这里的代码是已经解决了这个问题的呀,如果不加
# HttpTransportProperties.Authenticator basicAuth = new HttpTransportProperties.Authenticator(); 
#             basicAuth.setUsername("liuhand"); 
#             basicAuth.setPassword("dev110"); 
# //          basicAuthentication.setHost("xx.xx.x.xx");     
# //          basicAuthentication.setDomain("xx.xx.xx.xx");   
#             opts.setProperty(HTTPConstants.AUTHENTICATE, basicAuth); 
就会出现用户名密码错误的问题。
加上以上代码,把用户名和密码改成你项目中正确的用户名和密码就可以解决问题。
6 楼 songfantasy 2010-09-08  
我也遇到此问题,楼主是怎么解决的啊?谢谢。
5 楼 wujianjun 2009-12-16  
建议你先用 Altova XMLSpy 这个XML工具调用一次,如果能成功再用java去调。很多的错可能会出现在sap发布出来的wsdl文件里面。
4 楼 dabaosse 2009-12-14  
我用直接用wsdl2java生成的客户端
3 楼 dabaosse 2009-12-14  
你好,我调用SAP的服务时,报下面的错,好像跟解析报文有关吧? 帮忙看一下 谢谢:)

org.apache.axis2.AxisFault: com.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character 'P' (code 80) in DOCTYPE declaration; expected a space between PUBLIC keyword and public id
at [row,col {unknown-source}]: [1,22]
at org.apache.axis2.AxisFault.makeFault(AxisFault.java:430)
at org.apache.axis2.transport.TransportUtils.createSOAPMessage(TransportUtils.java:62)
at org.apache.axis2.description.OutInAxisOperationClient.handleResponse(OutInAxisOperation.java:353)
at org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:416)
at org.apache.axis2.description.OutInAxisOperationClient.executeImpl(OutInAxisOperation.java:228)
at org.apache.axis2.client.OperationClient.execute(OperationClient.java:163)
at mc_style.functions.soap.sap.document.sap_com.ZHR_RFC_PERNR_HIREServiceStub.ZhrRfcPernrHire(ZHR_RFC_PERNR_HIREServiceStub.java:158)
at mc_style.functions.soap.sap.document.sap_com.ZHR_RFC_PERNR_HIRETestCase.stub(ZHR_RFC_PERNR_HIRETestCase.java:87)
at mc_style.functions.soap.sap.document.sap_com.ZHR_RFC_PERNR_HIRETestCase.main(ZHR_RFC_PERNR_HIRETestCase.java:21)
2 楼 wujianjun 2009-12-14  
urn:sap-com:document:sap:rfc:functions 是sap里的那个表空间名称。把你们的异常信息贴出来吧!
1 楼 nocb 2009-12-12  
你好,你的代码很有用
客户端代码我一般是用eclipse plugin生成的,但是在生成 sap的web service时会报错,
提示 是和urn:sap-com:document:sap:rfc:functions 这个有关,请问该如何处理呢?
谢谢

相关推荐

Global site tag (gtag.js) - Google Analytics