远程SOAP Web服务不断断开连接

Gaj*_*res 15 java web-services fiddler jboss-eap-6 switchyard

简短的介绍

我正在使用JBoss SwitchYard连接到安全的远程SOAP Web服务.发出请求后出于某种原因; 远程Web服务正在停止任何进一步的通信; 所以我没有收到回复.

我需要一个想法或解决方案,这可能是一个问题.

错误

引起:java.net.SocketException:SocketException调用https://**********.asmx:来自服务器的文件意外结束

说明和说明

  • 远程Web服务使用自签名证书; 我已将服务器证书导入到我的本地信任库+ +我有我的其他证书(在我的密钥库中)以向远程服务器标识自己.
  • 感谢-Djavax.net.debug =所有 SSL调试日志和Wireshark日志我知道客户端和服务器都成功进行了SSL握手,客户端已成功发送请求.
  • 服务器还使用IP过滤来允许直接通信,并且我的IP被列入白名单.
  • 如果我尝试通过SoapUI发送相同的XML请求,它工作正常,我收到响应.您应该考虑到SoapUI只使用密钥库; 它被设置为始终信任远程服务,因此不需要或使用信任库.
  • 现在来了有趣的部分.如果我使用Fiddler(免费的网络调试代理)作为我的JBoss SwitchYard和远程Web服务之间的"中间人" (看看发生了什么),突然一切正常.
  • 直接连接和使用Fiddler作为代理之间的唯一区别在于,在实际连接中使用Connection = Keep-Alive头参数,在Fiddler情况下,使用Proxy-Connection = Keep-Alive参数.我不知道还有其他重大区别.
  • 如果我在SoapUI中手动更改这些头参数,我仍然会收到成功的响应.只有当我缺少SOAPActionContent-Type头参数时,连接才会失败,但它们在每种情况下都存在(并且是相同的).
  • 当我通过Wireshark观察这种通信时,我只能看到远程服务器正在停止进一步的通信(当JBoss Switchyard应用程序直接与远程Web服务通信时).
  • 我无法访问远程日志,也无法获取远程日志.所以我在盲目工作.
  • 在每种情况下(有或没有Fiddler)我正在使用公司代理来访问远程Web服务.此代理不是问题,因为其他SwitchYard应用程序正常工作.

工具

  • JBoss EAP 6.4
  • JBoss SwitchYard 2.0.1.redhat-621159

Ton*_*ony 5

例外

java.net.SocketException:服务器中的文件意外结束

此异常表示服务器已接受您的连接,这意味着您的SSL握手确实成功。但是服务器在获得响应之前关闭了连接(通过TCP重置或fin)。

重置通常在两种情况下发送:

  • 与配置的持久连接(保持活动连接)超过
  • 服务器重新启动丢失了连接

通常,持久连接具有两个配置:

Keep-Alive: timeout=15, max=100
Run Code Online (Sandbox Code Playgroud)

timeout表示以秒为单位的时间,max表示最大请求数。

连接与代理连接

让我们比较一下您描述的三种不同情况:

  • SoapUI:成功;?
  • 直接连接:失败;连接=保持活动
  • 提琴手:成功;代理连接=保持活动

在第三种情况下,如果我理解正确,则您与客户端之间以及代理与服务器之间的持久连接是不清楚的。

 client----->Proxy----->server
Run Code Online (Sandbox Code Playgroud)

意见建议

  • 尝试从响应(如此)中获取服务器的持久连接配置,以查看直接连接是否超过了时间或限制数
  • 尝试不使用持久连接: java -Dhttp.keepalive=false

参考


Piy*_*iya 1

问题可能是由于 SOAP 请求的标头无效或格式无效,您可以尝试如下代码

1 你需要HeaderHandlerResolver

 public class HeaderHandlerResolver implements HandlerResolver {

        public List<Handler> getHandlerChain(PortInfo portInfo) {
            List<Handler> handlerChain = new ArrayList<Handler>();

            HeaderHandler hh = new HeaderHandler();

            handlerChain.add(hh);

            return handlerChain;
        }
    }
Run Code Online (Sandbox Code Playgroud)

然后你需要添加HeaderHandler

 public class HeaderHandler implements SOAPHandler<SOAPMessageContext> {

        public boolean handleMessage(SOAPMessageContext smc) {

            Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

            if (outboundProperty.booleanValue()) {

                SOAPMessage message = smc.getMessage();

                try {

                    SOAPEnvelope envelope = smc.getMessage().getSOAPPart().getEnvelope();

                    SOAPHeader header = envelope.getHeader();
                    header.setPrefix("soapenv");
                    header.setAttribute("xmlns:wsa", "http://www.w3.org/2005/08/addressing");

                    SOAPElement security =
                            header.addChildElement("Security", "wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");

                    SOAPElement usernameToken =
                            security.addChildElement("UsernameToken", "wsse");
                    usernameToken.addAttribute(new QName("xmlns:wsu"), "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");

                    SOAPElement username =
                            usernameToken.addChildElement("Username", "wsse");
                    username.addTextNode("USERNAME");

                    SOAPElement password =
                            usernameToken.addChildElement("Password", "wsse");
                    password.setAttribute("Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText");
                    password.addTextNode("PASSWORD");

                    SOAPElement encode =
                            usernameToken.addChildElement("Nonce", "wsse");
                    encode.setAttribute("EncodingType", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary");
                    encode.addTextNode(generateNonce());

                    Calendar createdTime = new GregorianCalendar(TimeZone.getTimeZone("IST"));
                    Date todayDate = createdTime.getTime();
                    todayDate.setTime(todayDate.getTime()-20000000);

                    SOAPElement created = usernameToken.addChildElement("Created", "wsu");
                    created.addTextNode(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.sss'Z'").format(todayDate));

                    SOAPElement action = header.addChildElement("Action", "wsa");
                    //YOUR ACTION URL SHOULD BE in BELOW Text Content
                    action.setTextContent("SET HERE YOUR ACTION URL");

                    message.saveChanges();
                    message.writeTo(System.out);
                    System.out.println("");

                } catch (Exception e) {
                    e.printStackTrace();
                }

            } else {
                try {

                    //This handler does nothing with the response from the Web Service so
                    //we just print out the SOAP message.
                    SOAPMessage message = smc.getMessage();
                    message.writeTo(System.out);
                    System.out.println("");
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }


            return outboundProperty;

        }

        public Set getHeaders() {
            return null;
        }

        public boolean handleFault(SOAPMessageContext context) {
            return true;
        }

        public void close(MessageContext context) {
        }

        private static String generateNonce() throws NoSuchAlgorithmException, NoSuchProviderException, UnsupportedEncodingException {
            String dateTimeString = Long.toString(new Date().getTime());
            byte[] nonceByte = dateTimeString.getBytes();
            return Base64.encodeBase64String(nonceByte);
        }
    }
Run Code Online (Sandbox Code Playgroud)

现在终于是您调用 SOAP 服务的主类了

public class SoapClientClass {

    public static void main(String[] args) {

        ImplService service = new ImplService();
        HeaderHandlerResolver handlerResolver = new HeaderHandlerResolver();
        service.setHandlerResolver(handlerResolver);

        ResponseClass port = service.getPortClass();

        Response response = null;
        try {
            response = port.getServerMehotd("Params");
        } catch (PolicyException_Exception e) {
            e.printStackTrace();
        } catch (ServiceException_Exception e) {
            e.printStackTrace();
        }

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

另外,请确保从 wsdl 文件生成的代码是最新的,并且服务器位置 url 也正确。

希望它能解决您的问题