我为使用basicHttpBinding的WCF Web服务生成了Java客户端,一切正常。
然后,对于同一个Web服务,我生成了使用wsHttpBinding的Java客户端,我需要用户名和密码(没有用户并传递basicHttpBinding)并且它不起作用。我有例外: org.apache.axis2.AxisFault:com.ctc.wstx.exc.WstxEOFException:prolog中意外的EOF 在[row,col {unknown-source}]:[1,0]
这是我第一次为需要用户和传递的WCF Web服务创建Java客户端,因此我随机搜索并找到几种设置凭据的解决方案:
1
HttpTransportProperties.Authenticator
auth = new HttpTransportProperties.Authenticator();
auth.setPreemptiveAuthentication(true);
auth.setUsername("test");
auth.setPassword("test");
stub._getServiceClient().getOptions().setProperty(org.apache.axis2.transport.http.HTTPConstants.AUTHENTICATE,auth);
2
ServiceClient client = stub._getServiceClient();
SOAP11Factory factory = new SOAP11Factory();
OMNamespace SecurityElementNamespace = factory.createOMNamespace("http://schemas.xmlsoap.org/ws/2002/04/secext", "wlm");
OMElement usernameTokenEl = factory.createOMElement("UsernameToken", SecurityElementNamespace);
OMElement usernameEl = factory.createOMElement("Username", SecurityElementNamespace);
OMElement passwordEl = factory.createOMElement("Password", SecurityElementNamespace);
usernameEl.setText("test");
passwordEl.setText("test");
usernameTokenEl.addChild(usernameEl);
usernameTokenEl.addChild(passwordEl);
SOAPHeaderBlockImpl block = new SOAP11HeaderBlockImpl("Security", SecurityElementNamespace, factory);
block.addChild(usernameTokenEl);
client.addHeader(block);
3
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("test");
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("test");
omusertoken.addChild(omuserName);
omusertoken.addChild(omPassword);
omSecurityElement.addChild(omusertoken);
stub._getServiceClient().addHeader(omSecurityElement);
但他们都没有帮助。
你有什么想法吗?