我尝试使用CXF用户指南让它工作,但我没有运气.
我正在尝试使用java代码调用Web服务.
这由JAX-WS规范涵盖.基本上,在请求上下文中将用户名/密码设置为属性:
((BindingProvider)proxy).getRequestContext().put( BindingProvider.USERNAME_PROPERTY, "joe"); ((BindingProvider)proxy).getRequestContext().put( BindingProvider.PASSWORD_PROPERTY, "pswd");
运行时将它们放入HTTP标头中.
您可以提供自己的身份验证器.这样,如果WDSL本身受到基本HTTP身份验证的保护,它将起作用.
@WebServiceRef(wsdlLocation = "https://laka/sito?wsdl") static XxxService service; public static void main(String[] args) { Authenticator.setDefault(new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("user", "password".toCharArray()); } }); service = new XxxService(); Xxx port = service.getXxxPort(); // invoke webservice and print response XxxResponse resp = port.foo(); System.out.println(resp.toString()); }
有一个更好的方法:
从WSDL生成Java时,添加选项"-exsh true":
wsdl2java -exsh true -p edu.sharif.ce http://wsdl.ir/WebServices/WebService.asmx?WSDL
并在使用时添加UserCredential:
UserCredentials user = new UserCredentials(); user.setUserid("user"); user.setPassword("pass"); ResearchWebService_Service service = new ResearchWebService_Service(); ResearchWebService port = service.getResearchWebService(); port.addNewProject(newProject, user);