1

我在 Stackoverflow 上搜索了这个问题,发现了一些类似的问题,但没有一个能解决我的问题。我已经编译了所有“建议的”解决方案,但没有任何效果:-(我有一个 wsdl,我使用 adb client(Axis 2) 生成了客户端代码。wsdl 说这个请求将通过 Https url 发送。我能够成功使用wsdl到java创建一个存根。但是我不确定如何进行基本身份验证。告诉我细节的文档还说我的用户名和密码应该使用Base64编码。

使用的身份验证方法是 HTTP Basic 。用户名和密码需要以 base64 格式编码 - UTF8 字符集。

示例:用户名:密码 = “VXNlcm5hbWU6UGFzc3dvcmQ=”</p>

顺便说一句,我已经在 SOAP UI 中尝试过这个 wsdl,并且我得到了正确的响应,但是我的 java 代码无法正常工作

现在这里是wsdl

<?xml version="1.0" encoding="UTF-8"?>
<definitions targetNamespace="urn:OTSB2B" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="urn:OTSB2B" xmlns:intf="urn:OTSB2B" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <wsdl:types>
        <schema targetNamespace="urn:OTSB2B" xmlns="http://www.w3.org/2001/XMLSchema" xmlns:urn="urn:OTSB2B">
            <simpleType name="tn">
                <restriction base="string">
                    <length value="10"/>
                </restriction>
            </simpleType>
            <simpleType name="prov">
                <restriction base="string">
                    <length value="2"/>
                    <enumeration value="on"/>
                    <enumeration value="qc"/>
                </restriction>
            </simpleType>
            <element name="getPresaleByTN">
                <complexType>
                    <sequence>
                        <element name="tn" type="urn:tn"/>
                        <element name="prov" type="urn:prov"/>
                    </sequence>
                </complexType>
            </element>
            <element name="getPresaleByTNReturn" type="xsd:string"/>
            <element name="isAlive"/>
            <element name="isAliveReturn" type="xsd:boolean"/>
        </schema>
    </wsdl:types>
    <message name="isAliveRequest">
        <part element="impl:isAlive" name="isAlive"/>
    </message>
    <message name="getPresaleByTNRequest">
        <part element="impl:getPresaleByTN" name="getPresaleByTN"/>
    </message>
    <message name="isAliveResponse">
        <part element="impl:isAliveReturn" name="isAliveReturn"/>
    </message>
    <message name="getPresaleByTNResponse">
        <part element="impl:getPresaleByTNReturn" name="getPresaleByTNReturn"/>
    </message>
    <portType name="GetPresaleByTN">
        <operation name="getPresaleByTN">
            <input message="impl:getPresaleByTNRequest" name="getPresaleByTNRequest"/>
            <output message="impl:getPresaleByTNResponse" name="getPresaleByTNResponse"/>
        </operation>
        <operation name="isAlive">
            <input message="impl:isAliveRequest" name="isAliveRequest"/>
            <output message="impl:isAliveResponse" name="isAliveResponse"/>
        </operation>
    </portType>
    <binding name="DominoSoapBinding" type="impl:GetPresaleByTN">
        <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
        <operation name="getPresaleByTN">
            <wsdlsoap:operation soapAction=""/>
            <input name="getPresaleByTNRequest">
                <wsdlsoap:body use="literal"/>
            </input>
            <output name="getPresaleByTNResponse">
                <wsdlsoap:body use="literal"/>
            </output>
        </operation>
        <operation name="isAlive">
            <wsdlsoap:operation soapAction=""/>
            <input name="isAliveRequest">
                <wsdlsoap:body use="literal"/>
            </input>
            <output name="isAliveResponse">
                <wsdlsoap:body use="literal"/>
            </output>
        </operation>
    </binding>
    <service name="GetPresaleByTNService">
        <port binding="impl:DominoSoapBinding" name="Domino">
            <wsdlsoap:address location="https://b2b.ivv.bell.ca/ots-qualification-service-tn"/>
        </port>
    </service>
</definitions>

我试过这个:

GetPresaleByTNServiceStub stub = new GetPresaleByTNServiceStub();
            ServiceClient client = stub._getServiceClient();
            client.addStringHeader(new QName("userName"), "XXX");
            client.addStringHeader(new QName("password"), "YYYYYYYY");

            GetPresaleByTNServiceStub.GetPresaleByTN request = new GetPresaleByTN();
            Tn tn = new Tn();
            tn.setTn("4164390001");
            request.setTn(tn);
            request.setProv(Prov.on);

            GetPresaleByTNReturn response = stub.getPresaleByTN(request);
            System.out.println(response.getGetPresaleByTNReturn());

这给了我以下错误:

org.apache.axis2.AxisFault:添加字符串标头失败,您必须在 com.dinesh.bellAxis.App 的 org.apache.axis2.client.ServiceClient.addStringHeader(ServiceClient.java:434) 中拥有 QName 的 namespaceURI。主要(App.java:30)

然后我尝试了这个

GetPresaleByTNServiceStub stub = new GetPresaleByTNServiceStub();
            ServiceClient client = stub._getServiceClient();

            HttpTransportProperties.Authenticator basicAuth = new HttpTransportProperties.Authenticator();
            basicAuth.setUsername("XXX");
            basicAuth.setPassword("CCCCC");
            basicAuth.setPreemptiveAuthentication(true);

            stub._getServiceClient().getOptions().setProperty(HTTPConstants.AUTHENTICATE, basicAuth);

            GetPresaleByTNServiceStub.GetPresaleByTN request = new GetPresaleByTN();
            Tn tn = new Tn();
            tn.setTn("4164390001");
            request.setTn(tn);
            request.setProv(Prov.on);

            GetPresaleByTNReturn response = stub.getPresaleByTN(request);
            System.out.println(response.getGetPresaleByTNReturn());

这给了我以下错误:

org.apache.axis2.AxisFault:传输级别信息与 org.apache.axis2.AxisFault.makeFault(AxisFault.java:430) 的 SOAP 消息命名空间 URI 不匹配,位于 org.apache.axis2.transport.TransportUtils.createSOAPMessage(TransportUtils .java:90) 在 org.apache.axis2.description.OutInAxisOperationClient.handleResponse(OutInAxisOperation.java:353) 在 org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:416) 在 org.apache.axis2。 description.OutInAxisOperationClient.executeImpl(OutInAxisOperation.java:228) at org.apache.axis2.client.OperationClient.execute(OperationClient.java:163) at com.acn.client.GetPresaleByTNServiceStub.getPresaleByTN(GetPresaleByTNServiceStub.java:460)

接下来我尝试了这个:我认为这是不正确的,因为它是 WS 安全而不是基本身份验证,但是我用尽了所有选项

GetPresaleByTNServiceStub stub = new GetPresaleByTNServiceStub();
            ServiceClient client = stub._getServiceClient();

            OMFactory omFactory = OMAbstractFactory.getOMFactory();
            OMElement omSecurityElement = omFactory.createOMElement(new QName( "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Security", "wsse"), null);


            OMElement omusertoken = omFactory.createOMElement(new QName("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd", "UsernameToken", "wsu"), null);

            OMElement omuserName = omFactory.createOMElement(new QName("", "Username", "wsse"), null);
            omuserName.setText("XXXX");

            OMElement omPassword = omFactory.createOMElement(new QName("", "Password", "wsse"), null);
            omPassword.addAttribute("Type","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText",null );
            omPassword.setText("YYYYYYY");

            omusertoken.addChild(omuserName);
            omusertoken.addChild(omPassword);
            omSecurityElement.addChild(omusertoken);
            stub._getServiceClient().addHeader(omSecurityElement);

            GetPresaleByTNServiceStub.GetPresaleByTN request = new GetPresaleByTN();
            Tn tn = new Tn();
            tn.setTn("4164390001");
            request.setTn(tn);
            request.setProv(Prov.on);

            GetPresaleByTNReturn response = stub.getPresaleByTN(request);
            System.out.println(response.getGetPresaleByTNReturn());

这给出了以下错误:

线程“main”java.lang.IllegalArgumentException 中的异常:无法在 org.apache.axiom.om.impl.llom.OMElementImpl.handleNamespace(OMElementImpl.java:186) 处创建具有空命名空间名称的前缀元素。 axiom.om.impl.llom.OMElementImpl.(OMElementImpl.java:161) at org.apache.axiom.om.impl.llom.factory.OMLinkedListImplFactory.createOMElement(OMLinkedListImplFactory.java:126) at com.dinesh.bellAxis.App2 .main(App2.java:37)

我不知道接下来要做什么,我已经检查了 Apache Axis2 上的所有文档,并到处搜索,但可以让代码正常工作。

有什么建议么

4

2 回答 2

0

代码片段 1 和 3 不起作用,因为它们试图创建对 SOAP(片段 1)或 XML(片段 3)无效的消息。无论如何,他们尝试将 SOAP 标头添加到消息中,这不是基本身份验证的内容。

代码片段 2 看起来是正确的。从异常的堆栈跟踪(更准确地说是handleResponse方法的存在),您可以看到响应存在问题。错误消息可能表明响应的内容类型与响应中实际使用的 SOAP 版本不匹配。这意味着服务有问题,而不是客户端有问题。

于 2012-12-22T17:43:14.223 回答
0

经过大量试验和错误并在 Oracle 网站上查看 SAAj 教程后,我找到了答案。

我可以使用此字符串进行基本身份验证授权 = new sun.misc.BASE64Encoder().encode((“myUserName”+”:”+”myPassword”).getBytes()); headers.addHeader(“授权”, “基本” + 授权);

这是完整的教程 - http://www.javahabit.com/2014/10/17/quick-tutorial-saaj-api/

于 2014-10-20T13:31:00.733 回答