SOAP客户端 - ProtocolException:服务器重定向次数过多

时间:2017-10-27 16:16:11

标签: java web-services soap wsdl jax-ws

我尝试创建SOAP客户端以与服务器通信。使用C#,一切都顺利运行,但在Java上我仍然有一些例外。

我们公司落后于代理。 SOAP服务是基本身份验证的基础。

小故事: 我使用来自wsdl的JAX_WS生成Java生成模式的代码

User Settings

使用实现身份验证和某种cookie:

/**
 * This class was generated by the JAX-WS RI.
 * JAX-WS RI 2.2.9-b130926.1035
 * Generated source version: 2.2
 */
@WebServiceClient(name = "ServiceWs", targetNamespace = "http://www.web.org/Schema/Klient/Service", wsdlLocation = "https://app.web.org/KlientWS/ServiceWs.wsdl")
public class ServiceWs
        extends Service {
    public ServiceWs(URL wsdlLocation, QName serviceName, WebServiceFeature... features) {
        super(wsdlLocation, serviceName, features);
    }
    ...
}

并实施客户端

CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
Authenticator.setDefault(new Authenticator() {
    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
            System.setProperty("java.net.useSystemProxies", "true");

            System.setProperty("proxySet", "true");
            System.setProperty("proxyHost", localProxyHost);
            System.setProperty("proxyPort", localProxyPort);
            System.setProperty("proxyUser", localProxyUser);
            System.setProperty("proxyPassword", localProxyPassword);
            String prot = getRequestingProtocol().toLowerCase();

            // Requesting protocol
            System.setProperty(prot + ".proxyHost", localProxyHost);
            System.setProperty(prot + ".proxyPort", localProxyPort);
            System.setProperty(prot + ".proxyUser", localProxyUser);
            System.setProperty(prot + ".proxyPassword", localProxyPassword);

//          if (getRequestorType() == RequestorType.PROXY) {
//              if (getRequestingHost().toLowerCase().equals(localProxyHost.toLowerCase())) {
//                  if (Integer.parseInt(localProxyPort) == getRequestingPort()) {
//                      // Seems to be OK.
//                      return new PasswordAuthentication(localProxyUser, localProxyPassword.toCharArray());
//                  }
//              }
//          } else
            if (getRequestorType() == RequestorType.SERVER) {
                return new PasswordAuthentication(username, password.toCharArray());
            }
            return null;
        }
    });

在连接到wsdl文件时,只有 ProtocolException 失败并且重定向过多。

更短的版本

ServiceWs client;
try {
    URL url = new URL(endpointAddress);
    client = new ServiceWs(url);
} catch (IOException e) {
    client = new ServiceWs();
}

更长的版本

org.apache.cxf.service.factory.ServiceConstructionException: Failed to create service.
Caused by: java.net.ProtocolException: Server redirected too many  times (20)

我知道身份验证详细信息正确且网址正确,因为我可以手动连接到wsdl文件。

2 个答案:

答案 0 :(得分:3)

问题是,正如@beat在代理设置中建议的那样。我没有正确设置NTLM,因为我们的代理用户位于不同的域中。

要设置的代码是System.setProperty("http.auth.ntlm.domain", domain);,设置完成后,一切都按预期开始工作。

更多内容见J2SE Proxy Authentication

private void createAuthentication(String localProxyHost, String localProxyPort, 
    String localProxyUser, String localProxyPassword, String domain, 
    String username, String password) {

    if (!prepared) {
        System.setProperty("java.net.useSystemProxies", "true");

        System.setProperty("http.auth.ntlm.domain", domain);
        System.setProperty("proxyHost", localProxyHost);
        System.setProperty("proxyPort", localProxyPort);
        System.setProperty("proxyUser", localProxyUser);
        System.setProperty("proxyPassword", localProxyPassword);

        prepared = true;
        CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
        Authenticator.setDefault(new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password.toCharArray());
            }
        });
    }
}

答案 1 :(得分:2)

根据Polostor的要求,我试着写一个答案。 NTLMv2代理正在对用户进行身份验证,如果没有成功则重定向您。

由于我在不同站点的不同代理有很多问题,我建议使用proxy-vole。这将根据平台设置自动设置代理。