2013-05-17 22 views
1

我要访问一个Web服务,这是代码的样子:javax.xml.ws.Service在构造函数中失败,因为网站拥有用户名/密码

URL url = new URL("http://localhost:8080/sample?ver_=1.0"); 

QName qname = new QName("http://webservices.sample.com/sample", "sample"); 

javax.xml.ws.Service service = javax.xml.ws.Service.create(url, qname); 

初始化“服务”投行401未经授权的例外。

如果我使用的浏览器访问

http://localhost:8080/sample?ver_=1.0 

,一个窗口,询问用户名和密码弹出。

我试图使用Wireshark捕获数据包,并注意到服务的构造函数发送一个HTTP Get到IP地址但没有凭据。

如何确保服务构造函数对HTTP Get的调用包含用户名/密码?

我已经试图把这个电话之前,但它并没有帮助

Authenticator.setDefault(new Authenticator() { 
protected PasswordAuthentication getPasswordAuthentication() { 
return new PasswordAuthentication(
"username", 
"password".toCharArray()); 
} 
}); 

谢谢!

+0

是使用基本身份验证服务器?数据包中的认证头是什么? –

+0

是的,它的基本认证。这在标题上:Authorization:Basic racumin

回答

0

您需要获取服务端点。使用端点得到的RequestContext并添加认证头,这里是示例代码,根据您的Web服务和身份验证凭据修改。

Example example = service .getXXXPort(); 

Map<String, Object> req_ctx = ((BindingProvider)example).getRequestContext(); 
req_ctx.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "your web service URL here"); 

Map<String, List<String>> headers = new HashMap<String, List<String>>(); 
headers.put("Username", Collections.singletonList("username")); 
headers.put("Password", Collections.singletonList("password")); 
req_ctx.put(MessageContext.HTTP_REQUEST_HEADERS, headers); 
相关问题