繁体   English   中英

javax.xml.ws.Service在构造函数中失败,因为站点具有用户名/密码

[英]javax.xml.ws.Service fails in constructor because site has username/password

我想访问一个Web服务,这就是代码的样子:

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地址,但没有凭据。

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

我已经尝试过将它放在通话之前,但没有帮助

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

谢谢!

您需要获取服务端点。 使用端点get 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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM