【问题标题】:How to consume .NET web service in Netbeans which has a soap authentication header如何在具有肥皂身份验证标头的 Netbeans 中使用 .NET Web 服务
【发布时间】:2011-01-03 18:42:10
【问题描述】:

我正在尝试使用在 .NET 中创建的需要 SOAP 身份验证的 Web 服务。您最感兴趣的部分是:

<s:element name="SoapAuthenticationHeader" type="tns:SoapAuthenticationHeader" /> 
      <s:complexType name="SoapAuthenticationHeader"> 
        <s:sequence> 
          <s:element minOccurs="0" maxOccurs="1" name="Username" type="s:string" /> 
          <s:element minOccurs="0" maxOccurs="1" name="Password" type="s:string" /> 
        </s:sequence> 
        <s:anyAttribute /> 
      </s:complexType> 

我能够成功使用 Netbeans 中的 Web 服务。但我无法使用它,因为在自动生成的存根/方法中,我无法输入用户名和密码进行身份验证。

Netbeans 自动生成的存根包括以下内容:

JAX-WS

try { // Call Web Service Operation
            org.tempuri.TeleCast service = new org.tempuri.TeleCast();
            org.tempuri.TeleCastSoap port = service.getTeleCastSoap();
            // TODO initialize WS operation arguments here
            int campaignid = 0;
            java.lang.String to = "";
            java.lang.String from = "";
            java.lang.String subject = "";
            java.lang.String body = "";
            java.lang.String uniqueid = "";
            // TODO process result here
            boolean result = port.queueRealTimeEmail(campaignid, to, from, subject, body, uniqueid);
            System.out.println("Result = "+result);
        } catch (Exception ex) {
            // TODO handle custom exceptions here
        }

JAX-RPC

try { // This code block invokes the TeleCastSoap:queueRealTimeEmail operation on web service
            telecastclient.TeleCast teleCast = new telecastclient.TeleCast_Impl();
            telecastclient.TeleCastSoap teleCastSoap = teleCast.getTeleCastSoap();
            teleCastSoap.queueRealTimeEmail(/* TODO enter operation arguments*/);
        } catch(javax.xml.rpc.ServiceException ex) {
            java.util.logging.Logger.getLogger(telecastclient.TeleCast.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch(java.rmi.RemoteException ex) {
            java.util.logging.Logger.getLogger(telecastclient.TeleCast.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch(Exception ex) {
            java.util.logging.Logger.getLogger(telecastclient.TeleCast.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }

至少当我使用 JAX-WS 方法时,我得到了一个名为 SoapAuthenticationHeader 的类,我可以在其中设置用户名和密码。但我不知道如何在调用 Web 服务之前或期间传递 SoapAuthenticationHeader 对象以执行各种操作。

org.tempuri.SoapAuthenticationHeader auth = new SoapAuthenticationHeader();
            auth.setUsername("username");
            auth.setPassword("password");

我在 JAX-RPC 方法中没有这个选项。

对此的任何意见将不胜感激。提前感谢您的宝贵时间。

【问题讨论】:

    标签: java web-services authentication soap header


    【解决方案1】:

    在默认的 Netbeans IDE 中似乎没有优雅的方法可以做到这一点。

    我对 Eclipse 进行了同样的尝试,在我的所有搜索中,我被引导到了这个 Apache Axis2 - Eclipse Eclipse 插件的代码生成器向导指南(我仍然无法发布链接因为我是新手)。

    在使用上述插件(WSDL2Java 的某种 Eclipse 插件)时,它生成了存根以及适当的 SoapAuthenticationHeader 对象,我可以传递这些对象进行验证。

    所以我现在的代码是:

    TeleCastStub stub = new TeleCastStub();
    
    SoapAuthenticationHeader auth = new SoapAuthenticationHeader();
    auth.setUsername(username);
    auth.setPassword(password);
    
    QueueRealTimeEmail pageObj = new QueueRealTimeEmail();
    pageObj.setFrom(from);
    pageObj.setTo(to);
    pageObj.setSubject(subject);
    pageObj.setBody(body);
    
    SoapAuthenticationHeaderE authE = new SoapAuthenticationHeaderE();
    authE.setSoapAuthenticationHeader(auth);
    
    QueueRealTimeEmailResponse response = stub.queueRealTimeEmail(pageObj, authE);
    

    效果很好。

    现在这又引出了我几个问题:

    1. 我采用的方式,使用 axis2 插件,我的代码 sn-p, 这是正确的方法吗? 有时我会觉得我 只需将 hack 放在一起即可获得 工作完成了,我总是很感兴趣 寻找正确的方法 让我接下来可以做得更好 时间。
    2. 当我使用内置代码时 用eclipse生成向导,我 注意到它有 Axis 的选项 运行时而不是 Axis2。所以现在我的 问题是;与旧版本的 Axis 或带有预建工具的 IDE,使用 Web 服务 特殊的身份验证标头只是 做不到?更多的是架构限制吗?

    再次感谢您的宝贵时间。

    这块板子很棒,所以!无数次救了我的皮。所以现在轮到我回馈社区了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多