【发布时间】:2012-12-22 05:38:55
【问题描述】:
我在 Stackoverflow 上搜索了这个问题,发现了一些类似的问题,但没有一个能解决我的问题。我已经编译了所有“建议”的解决方案,但没有任何效果:-( 我有一个 wsdl,我使用 adb 客户端(Axis 2)生成了客户端代码。 wsdl 表示此请求将通过 Https url 发送。我能够使用 wsdl to java 成功创建存根。但是我不确定如何进行基本身份验证。告诉我详细信息的文档还说我的用户名和密码应该使用 Base64 编码。
使用的身份验证方法是 HTTP Basic 。用户名和 密码需要以 base64 格式编码 – UTF8 字符 设置。
示例:用户名:密码 = “VXNlcm5hbWU6UGFzc3dvcmQ=”
顺便说一句,我已经在 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:添加字符串头失败,你必须 QName 的 namespaceURI 位于 org.apache.axis2.client.ServiceClient.addStringHeader(ServiceClient.java:434) 在 com.dinesh.bellAxis.App.main(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:传输级别信息不匹配 SOAP 消息命名空间 URI 位于 org.apache.axis2.AxisFault.makeFault(AxisFault.java:430) 在 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) 在 org.apache.axis2.client.OperationClient.execute(OperationClient.java:163) 在 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());
这会产生以下错误:
线程“主”java.lang.IllegalArgumentException 中的异常:不能 创建一个带有空命名空间名称的前缀元素 org.apache.axiom.om.impl.llom.OMElementImpl.handleNamespace(OMElementImpl.java:186) 在 org.apache.axiom.om.impl.llom.OMElementImpl.(OMElementImpl.java:161) 在 org.apache.axiom.om.impl.llom.factory.OMLinkedListImplFactory.createOMElement(OMLinkedListImplFactory.java:126) 在 com.dinesh.bellAxis.App2.main(App2.java:37)
我不知道接下来要做什么,我已经检查了 Apache Axis2 上的所有文档,并在所有地方搜索过,但可以让代码正常工作。
任何建议
【问题讨论】:
-
我也遇到了这个问题,因为我正在向 WCF 服务的服务发送soap响应并且我得到 org.apache.axis2.AxisFault: Failed to add string header, you must have namespaceURI for QName。任何可能解决此问题的工作将不胜感激。
标签: web-services https client axis2 axis