如何在具有soap身份验证标头的Netbeans中使用.NET Web服务

时间:2011-01-03 18:42:10

标签: java web-services authentication soap header

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

<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方法中没有这个选项。

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

1 个答案:

答案 0 :(得分:0)

在默认的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插件,我的代码片段, 这是正确的方法吗? 有时候我会感觉到这一点 只是把一个黑客放在一起得到了 完成工作,我总是感兴趣 找到正确的方法 事情,所以我可以做得更好 时间。
  2. 当我使用内置代码时 和eclipse一代精灵,我 注意到它有Axis的选项 运行时而不是Axis2。所以现在我的 问题是;随着旧版本的 轴或预制的工具 IDE,使用Web服务 特殊身份验证标头 不能做?这更像是一种架构限制吗?
  3. 再次感谢你的时间。

    这个伟大的董事会,所以!已经多次保存我的隐藏。所以轮到我现在回馈社区。

相关问题