我有WSDL
.我需要进行HTTP
基本(抢先)身份验证.该怎么办?
我试过了 :
Authenticator myAuth = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("user", "pass".toCharArray());
}
};
Authenticator.setDefault(myAuth);
Run Code Online (Sandbox Code Playgroud)
但它不起作用:引起:
java.io.IOException:服务器返回HTTP响应代码:401为URL ..
PS我使用Apache CXF 2.6.2和JBoss 5.0.1
Pau*_*nis 15
您为身份验证指定的内容是不够的.你应该做这样的事情:
private YourService proxy;
public YourServiceWrapper() {
try {
final String username = "username";
final String password = "password";
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
username,
password.toCharArray());
}
});
URL url = new URL("http://yourserviceurl/YourService?WSDL");
QName qname = new QName("http://targetnamespace/of/your/wsdl", "YourServiceNameInWsdl");
Service service = Service.create(url, qname);
proxy = service.getPort(YourService.class);
Map<String, Object> requestContext = ((BindingProvider) proxy).getRequestContext();
requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, url.toString());
requestContext.put(BindingProvider.USERNAME_PROPERTY, username);
requestContext.put(BindingProvider.PASSWORD_PROPERTY, password);
Map<String, List<String>> headers = new HashMap<String, List<String>>();
requestContext.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
} catch (Exception e) {
LOGGER.error("Error occurred in web service client initialization", e);
}
}
Run Code Online (Sandbox Code Playgroud)
属性:
?WSDL
扩展名的Web服务的URL .WSDL
文件中的目标命名空间.第二:你的服务名称来自WSDL
.然后,您将能够像这样调用您的Web服务方法:
proxy.whatEverMethod();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
10987 次 |
最近记录: |