java调用SAP PI webservice ,用户名密码认证

package client;
/**
* power by mxcode 2015.10.05
*/

import java.net.URL;

import javax.xml.namespace.QName;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.commons.lang.StringUtils;

public class axis1CallPIWebservice {

private static Call call = null;

public static void main(String[] args) {
    //PI webservice地址
    String webServiceAddr = "http://10.114.20.14:50000/XISOAPAdapter/MessageServlet?channel=:BS_OA:CC_PORTAL_OA_NoticeInfo_OUT&version=3.0&Sender.Service=BS_OA&Interface=urn%3Aportal%3Aoa%3Aorgperappinf%5EMI_PORTAL_OA_NoticeInfo_OUT";
    //调用方法名
    String webServiceMethod = "addNoticeInfo";
    //传入参数名
    String[] inputNames = {"xml"};
    //传入参数类型
    String[] inputXmlTypes = {"Constants.XSD_STRING"};
    //命名空间
    String targetNamespace = "http://webservice.smsplatform.haiyisoft.com";
    //传入值
    Object[] inputValues = { "<?xml version=\"1.0\" encoding=\"UTF-8\"?><ParamsXml><Params><AgentID>GDDX</AgentID><DepartureAirport>CAN</DepartureAirport><ArriveAirport>PEK</ArriveAirport><DepartureDate>2014-01-18</DepartureDate><CarrierCode>CZ</CarrierCode><FS>D</FS></Params></ParamsXml>" };
    //返回值类型
    Class<?>[] returnJavaTypes = {String.class};
    //验证用户名
    String username = "test"; 
    //密码
    String password = "2015test";
    //无特殊含义
    String soapActionURI ="http://10.114.20.14:50000";
    //调用
    Object geObjectByAxis2RPCClient = getObjectByAxisCallClient(webServiceAddr, webServiceMethod,soapActionURI, targetNamespace, inputValues,inputNames,inputXmlTypes, returnJavaTypes, username, password);

    System.out.println(geObjectByAxis2RPCClient);
}

public static Object getObjectByAxisCallClient(String webServiceAddr, String webServiceMethod, String soapActionURI,
        String targetNamespace, Object[] inputValues, String[] inputNames, String[] inputXmlTypes, Class<?>[] inputJavaTypes, 
        String username, String password) {
    Object resObj = null;
    try {
        if (call == null) {
            Service service = new Service();
            call = (Call) service.createCall();
        }
        // 设置wsdl
        call.setTargetEndpointAddress(new URL(webServiceAddr));
        // 定义参数对象
        call.setUseSOAPAction(true);
        call.setSOAPActionURI(soapActionURI + webServiceMethod);
        // 设置访问的方法名

// call.setOperationName(webServiceMethod);
call.setOperationName(new QName(targetNamespace, webServiceMethod));//call是axis1产生的Call对象,namespaceURI应该是http://util.stat2,methodName应该是调用的方法名称

        call.addParameter(new QName(targetNamespace, inputNames[0]),org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN);
        call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);

        call.setTimeout(30000);

        // 访问权限验证.
        if (StringUtils.isNotEmpty(username) && StringUtils.isNotEmpty(password)) {
            call.setUsername(username);
            call.setPassword(password);
        }
        resObj = call.invoke(inputValues);
    } catch (Exception e) {
        // TODO Auto/generated catch block

// String message = messageFormat(“使用AxisCallClient调用WebService地址{{0}}方法{{1}}出现错误!”, webServiceAddr, webServiceMethod);
// logger.error(message, e);
// throw new InterfaceException(message);
e.printStackTrace();
}
return resObj;
}
}

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
调用 Java WebService 时也需要提供认证信息。下面是一个使用 Java 调用 WebService 的示例代码,包含了认证信息: ``` import java.net.URL; import javax.xml.namespace.QName; import javax.xml.ws.BindingProvider; import com.example.webservice.MyService; import com.example.webservice.MyServicePortType; public class MyWebServiceClient { public static void main(String[] args) { try { // WebService 的 URL URL url = new URL("https://www.example.com/webservice?wsdl"); // WebService 的 QName QName qname = new QName("http://example.com/webservice", "MyService"); // 创建 WebService 客户端 MyService service = new MyService(url, qname); MyServicePortType port = service.getMyServicePort(); // WebService认证信息 String username = "your_username"; String password = "your_password"; // 设置认证信息 BindingProvider bindingProvider = (BindingProvider) port; bindingProvider.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, username); bindingProvider.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, password); // 调用 WebService String result = port.getData("value1", "value2"); // 处理返回的数据 System.out.println("Result: " + result); } catch (Exception e) { e.printStackTrace(); } } } ``` 在这个示例中,我们使用了 Javajavax.xml.ws 包提供的类来创建 WebService 客户端。我们还将用户名密码作为参数传递给 BindingProvider.getRequestContext().put() 方法,以实现身份认证。最后,我们调用 WebService 的方法并处理返回的数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值